How to remove white-space from a String
September 2nd, 2009No Comments
/*** replace multiple whitespaces between words with single blank */
String value = before.trim().replaceAll("[ ][ ]*", " ");
or
//before : hi how are you
//after: hi how are you
/* remove leading whitespace */
//before : hi how are you
//after: hi how are you
/* remove trailing whitespace */
//before : hi how are you
//after: hi how are you
/** Trims all white spaces from an input string. */
String[] trimString = s.trim().split(" ");
String trimmedString = "";
for (int i = 0; i < trimString.length; i++){
trimmedString = trimmedString + trimString[i];
}
return trimmedString;
}
