This is an extension of my earlier question where i was using normalizeSpace(). But since this available only from api 26. How do we remove the whitespaces for earlier versions
1 Answer
If going back to basics I would suggest using the trim() function followed by a regex replacement using replaceAll() that matches instances of the space characters i.e. (\\s+)
and replaces them with a single space.
Example:
String str = ' how is the weather today ';
str = str.trim();
str = str.replaceAll('(\\s+)', ' ');
return str;