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
Amit Visapurkar 5Amit Visapurkar 5 

how to remove blank lines using apex

String 1

String 2

I want to remove the blank line between String 1 and String 2.

Please help its urgent 
Nayana KNayana K
You mean blank space? Can you provide some examples?
Amit Visapurkar 5Amit Visapurkar 5
There is a text area which gets updated with some data on a new line. Suppose there are three Strings.
String1
String2
String3

If String2 gets removed because of some update then the output is

String1

String 3

I want the output to be 

String1
String3

I need to remove the blank line.
 
SandhyaSandhya (Salesforce Developers) 
Hi Amit,

You can use below code.
 
(TextAreaField name).trim().replaceAll('\s+',' ')

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
Amit Visapurkar 5Amit Visapurkar 5
@Sandhya

I tried your solution but it is giving the following error

 Invalid string literal '\s+'. Illegal character sequence '\s' in string literal

 
SandhyaSandhya (Salesforce Developers) 
Hi Amit,

Please try this.
(TextAreaField name).trim().replaceAll('\\s+','');

Thanks and Regards
sandhya
Amit Visapurkar 5Amit Visapurkar 5
@Sandhya

Tried that also but not giving desired result. 
IT Support 192IT Support 192
I know this is an old thread, but this is exactly what I'm trying to do.  Did you figure it out @Amit Visapurkar 5