Here is a very small and simple code to remove superfluous whitespaces (leading, in between but leave at least one and at the end).
String test = ” Hello World here I am. “;
test = test.replaceAll(“\\s+”, ” “).trim();
Result is: “Hello World here I am.”;
I have seen a lot of other implementations that will also work. But they often use the regex package, which produces too much overhead and is not needed for such small issue.
Advertisement