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
sheila srivatsavsheila srivatsav 

How to remove white spaces lightning

I have a piece of code in my lightning component as follows

pCodeShortDesc = pCodeShortDesc.subtring(0,28);

this return a string like "Nebuent    Inh 300 mg"

I want to remove the two extra white spaces in the above.

Please let me know urgent how to do.

thanks
sheila
Best Answer chosen by sheila srivatsav
NagendraNagendra (Salesforce Developers) 
Hi,

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;

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi,

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;

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra 
This was selected as the best answer
sheila srivatsavsheila srivatsav
Hi Nagendra
sorry I did not phrase question properly

Inside the controller of lightning component the data is coming from api call.
pCodeShortDesc = pCodeShortDesc.subtring(0,28);

this return a string like "Nebuent    Inh 300 mg"

Now in UI, when I hover (i.e keep the mouse) it shows as Nebuent    Inh 300 mg

I debugged and saw that value from api call is Nebuent Inh 300 mg  but I dnot udnerstand when I hover it shows Nebuent    Inh 300 mg

please help me out very earliest

sheila
Deepali KulshresthaDeepali Kulshrestha
Hi sheila,

Please refer to these links it may be helpful to you.
https://salesforce.stackexchange.com/questions/1726/remove-whitespaces-in-a-string
https://developer.salesforce.com/forums/?id=906F000000091cXIAQ

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha