Skip to main content
added 197 characters in body
Source Link
Jon Hazan
  • 2.9k
  • 18
  • 29

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 charactercharacters i.e. /(\s+\\s+)/ and replaces itthem with a single space.

Example:

String str = ' how is   the weather today ';

str = str.trim();

str = str.replaceAll('(\\s+)', ' ');

return str;

I would suggest using the trim function followed by a regex replacement that matches instances of the space character i.e. /(\s+)/ and replaces it with a single space

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;
Source Link
Jon Hazan
  • 2.9k
  • 18
  • 29

I would suggest using the trim function followed by a regex replacement that matches instances of the space character i.e. /(\s+)/ and replaces it with a single space