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
shinerajbshinerajb 

How to remove space from a string?

Hai,

 

How to remove space from a string?

 

Shine

Best Answer chosen by Admin (Salesforce Developers) 
marko.tomicmarko.tomic

In such cases the best thing would be to introduce yourself with regular expressions. It's simple, it's powerfull and it works.

 

String sText = 'fdsafas fdsafds f     fdsafas  fdafdsa  ';
 sText= sText.replaceAll( '\\s+', '');
System.debug('*********************' + sText);

 

WIth \\s+ it will find all spaces in the string and replace it with empty string.

All Answers

kiranmutturukiranmutturu

"trim" in general...this is an instance method and remove leading and trailing white spaces

 

string str = '  hai';

str =  str.trim(); will gives you 'hai'

Anup JadhavAnup Jadhav

You can use the instance method "trim" on the String class.

 

According to the docs:

 

"Returns a copy of the string that no longer contains any leading or trailing white space characters.

Leading and trailing ASCII control characters such as tabs and newline characters are also removed. Whitespace and control characters that aren’t at the beginning or end of the sentence aren’t removed."

 

// Example

String str = '    whatever    ';

 

System.debug('the trimmed output is: ' + str.trim()); // outputs : the trimmed output is:whatever

 

Hope this helps!!

Anup JadhavAnup Jadhav

The trim method is only useful if you want to remove leading and/or trailing spaces. If you want to remove whitespace between a string (for e.g. 'remove this space'. Then you'd have to use 'split' as follows:

 

// remove space between strings example

String str = 'remove this space';

String[] splitString = str.split(' ');

 

for(String s: splitString) {

   str = str+s;

}

 

system.debug('the string is: '+str); // outputs - the string is: removethisspace

marko.tomicmarko.tomic

In such cases the best thing would be to introduce yourself with regular expressions. It's simple, it's powerfull and it works.

 

String sText = 'fdsafas fdsafds f     fdsafas  fdafdsa  ';
 sText= sText.replaceAll( '\\s+', '');
System.debug('*********************' + sText);

 

WIth \\s+ it will find all spaces in the string and replace it with empty string.

This was selected as the best answer
SFDC_LearnerSFDC_Learner
 "XYZ": "{\n  \"Test\" : \"S\",\n  \"SK\" : \"C\",\n  \"DD\" : \"A\",\n  \"AA\" : \"V\"\n}"
I am using json method to build the json request to post to the client server.
But in json i am having the literals \n and \.
This json request is not in the string format so i can not replace \n and \ with null value.
How can i replace the \n and \ with null value (request should be in json format).
Rune_WaageRune_Waage
I see this is an old post and since 2012 Salesforce has come with the String method deleteWhitespace().
String txt = ' How Are You Today?';
System.debug(txt.deleteWhitespace());

The code above will return: HowAreYouToday?