Java Utililty Methods String Whitespace Normalize

List of utility methods to do String Whitespace Normalize

Description

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

Method

StringnormaliseWhitespace(String string)
normalise Whitespace
StringBuilder sb = new StringBuilder(string.length());
boolean lastWasWhite = false;
boolean modified = false;
int l = string.length();
for (int i = 0; i < l; i++) {
    int c = string.codePointAt(i);
    if (Character.isWhitespace(c)) {
        if (lastWasWhite) {
...
StringnormaliseWhitespace(String string)
normalise Whitespace
return string == null ? "" : string.replaceAll("\\s+", " ");
StringnormaliseWhitespace(String string)
Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters (e.g.
StringBuilder sb = new StringBuilder(string.length());
appendNormalisedWhitespace(sb, string, false);
return sb.toString();
StringnormalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)
normalize Indentation From Whitespace
int spacesCnt = 0;
for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == '\t') {
        spacesCnt += tabSize;
    } else {
        spacesCnt++;
StringBuilder result = new StringBuilder();
if (!insertSpaces) {
    long tabsCnt = Math.round(Math.floor(spacesCnt / tabSize));
    spacesCnt = spacesCnt % tabSize;
    for (int i = 0; i < tabsCnt; i++) {
        result.append('\t');
for (int i = 0; i < spacesCnt; i++) {
    result.append(' ');
return result.toString();
StringnormalizeWhitespace(final String str)
Substitutes all whitespace with " "
if (str != null) {
    return str.replaceAll("\\s+", " ");
return "";
StringnormalizeWhitespace(String dirtyString)
Trim string and convert all multi whitespace regions to a single space.
if (dirtyString == null) {
    return null;
if (dirtyString.length() < 1) {
    return dirtyString;
StringBuffer buff = new StringBuffer();
int dsl = dirtyString.length();
...
StringnormalizeWhiteSpace(String input)
normalize White Space
return input.replaceAll("\\s+", " ").replaceAll("([^\\w])\\s+", "$1");
StringnormalizeWhiteSpace(String input)
normalize White Space
if (input == null) {
    return null;
StringBuffer result = new StringBuffer();
boolean atStart = true;
boolean whitespaceToInsert = false;
for (int i = 0; i < input.length(); i++) {
    char next = input.charAt(i);
...
StringnormalizeWhitespace(String orig)
Replace any string of whitespace characters with a single space, and remove whitespace from the beginning and end of the string.
return orig.replaceAll("\\s+", " ").trim();
StringnormalizeWhitespace(String s)
Removes all leading and trailing whitespace from a string, and replaces all internal whitespace with a single space character.
String normalized = "";
if (s != null) {
    s = s.trim();
    boolean prevIsWhitespace = false;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        boolean thisIsWhitespace = (c <= 0x20);
        if (!(thisIsWhitespace && prevIsWhitespace)) {
...