function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SudhanwaSudhanwa 

trim white spaces between two words

There is a problem in my search function. When I enter more than a single white space between two words in the key word, it does not retrieve the record.

 

For example, I have this account called 'Pink Floyd'. When I try to search through my search function by entering the key words as 'Pink    Floyd', it does not match. Any work around for trimming the white spaces?

 

Thanks,

Sudhanwa.

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

You could use a regular expression to remove any multiple white spaces before doing search:e.g.

 

string mySearch = 'abc   123';

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

//now do search 

 

R. 

All Answers

aalbertaalbert

You could use the LIKE clause and replace whitespace with %. You could also have the user select a exact or wildcard search to determine if you want to just use the search string they entered exactly or the LIKE Clause with % replacing whitespace? 

 

Richie DRichie D

You could use a regular expression to remove any multiple white spaces before doing search:e.g.

 

string mySearch = 'abc   123';

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

//now do search 

 

R. 

This was selected as the best answer
SudhanwaSudhanwa

Thanks a ton buddy. It worked.

 

Cheers,

Sudhanwa,

Nisha Verma 11Nisha Verma 11
Thanks a lot Richie D. It worked .