String (String string) { StringBuilder result = null; for (int i = 0, length = string.length(); i < length; i++) { char c = string.charAt(i); if (c == ' ' || c == '\t' || c == '\f' || c == '\n' || c == '\r') { // jls-3.6 if (result == null) { result = new StringBuilder(string.substring(0, i)); } } else if (result != null) { result.append(c); } } return result == null ? string : result.toString(); }