Find your content:

Search form

You are here

Remove whitespaces in a string

 
Share

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


Attribution to: Prady

Possible Suggestion/Solution #1

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;

Attribution to: Jon Hazan
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1726

My Block Status

My Block Content