String (@NotNull CharSequence s) { StringBuilder result = new StringBuilder(); boolean space = false; for (int i = 0, length = s.length(); i < length; i++) { char ch = s.charAt(i); if (isWhiteSpace(ch)) { if (!space) space = true; } else { if (space && result.length() > 0) result.append(' '); result.append(ch); space = false; } } return result.toString(); }