List of utility methods to do String Whitespace Collapse
String | collapseWhitespace(String string) collapse Whitespace if (string.isEmpty()) { return string; char space = ' '; char newline = '\n'; if (string.indexOf(newline) == -1 && string.indexOf('\r') != -1) { newline = '\r'; StringBuilder s = new StringBuilder(string.length()); char previousChar = newline; for (char currentChar : string.toCharArray()) { if (currentChar == newline && previousChar == newline) continue; if (currentChar == space && previousChar == space) continue; if (currentChar == space && previousChar == newline) continue; if (currentChar == newline && previousChar == space) { s.deleteCharAt(s.length() - 1); s.append(currentChar); previousChar = currentChar; if (previousChar == space || previousChar == newline) { s.deleteCharAt(s.length() - 1); return s.toString(); |
String | collapseWhiteSpace(String string) After the processing implied by replace, contiguous sequences of #x20's are collapsed to a single #x20, and leading and trailing #x20's are removed. if (string == null) { return null; String replaced = replaceWhiteSpace(string); char[] chars = replaced.toCharArray(); StringBuffer result = new StringBuffer(replaced.length()); boolean needSpace = false; for (char c : chars) { ... |
String | collapseWhiteSpace(String text) collapse White Space int len = text.length(); int s; for (s = 0; s < len; s++) if (isWhiteSpace(text.charAt(s))) break; if (s == len) return text; StringBuffer result = new StringBuffer(len); ... |
String | collapseWhitespace(String text) Attempts to replace all sequences matching \s+ with a single space. String temp = text.replaceAll("[\\t ]+", " ").replaceAll("^ $", "").replaceAll("\r", ""); while (temp.indexOf("\n\n") >= 0) { temp = temp.replaceAll("\n\n", "\n"); return temp; |
String | collapseWhiteSpace(StringBuffer oriText) collapse multi-whitespace into one whitespace in the original text. if (oriText == null) { return null; int oriLen = oriText.length(); StringBuffer newText = new StringBuffer(oriLen); int wsCount = 0; char lastWs = ' '; for (int i = 0; i <= oriLen; i++) { ... |
String | collapseWhiteSpaces(String str) Collapses consecutive white spaces into one space. String result = null; if (str != null) { StringBuffer buffer = new StringBuffer(); boolean isInWhiteSpace = false; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isWhitespace(c)) { isInWhiteSpace = true; ... |