/*** replace multiple whitespaces between words with single blank */ String before=" hi how are you "; String value = before.trim().replaceAll("[ ][ ]*", " "); or String value = before.replaceAll("\\b\\s{2,}\\b", " "); //before : hi how are you //after: hi how are you /* remove leading whitespace */ String value= before.replaceAll("^\\s+", ""); //before : hi [...]
↧