Java Utililty Methods String Whitespace Collapse

List of utility methods to do String Whitespace Collapse

Description

The list of methods to do String Whitespace Collapse are organized into topic(s).

Method

StringcollapseSpaces(String argStr)
Remove/collapse multiple spaces.
char last = argStr.charAt(0);
StringBuffer argBuf = new StringBuffer();
for (int cIdx = 0; cIdx < argStr.length(); cIdx++) {
    char ch = argStr.charAt(cIdx);
    if (ch != ' ' || last != ' ') {
        argBuf.append(ch);
        last = ch;
return argBuf.toString();
StringcollapseSpaces(String s)
collapse Spaces
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    final char c = s.charAt(i);
    if (!isSpace(c)) {
        stringBuilder.append(c);
return stringBuilder.toString();
...
StringcollapseSpaces(String text, char[] spaceCharacters)
Collapses spaces in the text, but does not trim the resulting string.
if (text == null || text.length() == 0)
    return text;
StringBuffer result = new StringBuffer();
char[] textChars = text.toCharArray();
boolean wasSpace = false;
for (int i = 0; i < textChars.length; i++) {
    if (!isCharacterInArray(textChars[i], spaceCharacters)) {
        result.append(textChars[i]);
...
StringcollapseWhiteSpace(CharSequence value)
Replaces successive XML space characters by a single space character (' ') then removes leading and trailing space characters if any.
StringBuffer buffer = new StringBuffer();
compressWhiteSpace(value, buffer);
int last = buffer.length() - 1;
if (last >= 0) {
    if (buffer.charAt(last) == ' ') {
        buffer.deleteCharAt(last);
        --last;
    if (last >= 0 && buffer.charAt(0) == ' ')
        buffer.deleteCharAt(0);
return buffer.toString();
StringcollapseWhiteSpace(final String s)
collapse White Space
return s.replaceAll("[\\s]+", " ");
StringcollapseWhiteSpace(String content)
Collapse all whitespace in a given content into a single whitespace.
return content.replaceAll("\\s+", " ");
StringcollapseWhitespace(String in, char replacementChar)
collapse Whitespace
StringBuilder sb = new StringBuilder();
int size = in.length();
boolean lastWasSpace = true;
int i = 0;
while (i < size) {
    char c = in.charAt(i++);
    boolean ws = Character.isWhitespace(c);
    if (ws) {
...
StringcollapseWhitespace(String s)
Collapse continuous whitespace into one single " ".
int len = s.length();
StringBuilder b = new StringBuilder(len);
for (int i = 0; i < len; i++) {
    char c = s.charAt(i);
    if (!Character.isWhitespace(c)) {
        b.append(c);
    } else {
        b.append(' ');
...
StringcollapseWhitespace(String s, boolean trim)
collapse Whitespace
char chars[] = new char[s.length()];
s.getChars(0, chars.length, chars, 0);
int r = 0;
int w = 0;
while (r < chars.length) {
    char c = chars[r++];
    if (isCollapsibleSpace(c)) {
        int collapseLength = 1;
...
StringcollapseWhitespace(String str)
collapse Whitespace
if (!needsCollapsing(str)) {
    return str;
int len = str.length();
char[] buf = new char[len];
str.getChars(0, len, buf, 0);
int leadingWSLen = 0;
int trailingWSLen = 0;
...