I retrieve the description field of Event and Task objects. Some descriptions contain copy pasted email and so empty lines exist in the description. I want to replace all empty lines with a single space. Firstly I convert the field content to a string like.

String.valueOf(t.Description).replace('\\s+, ' ')  

where t = task. I have tested many cases but does not seem to work. how can I do that? Thank you in advance!

2 Answers 2


Try using \r and \n. These are the standard newline characters that salesforce uses.

String.valueOf(t.Description).replace('\n', '').replace('\r', '');

You can read more at a related salesforce stackexchange post here.


Thanks batter.cord I found a solution. String.valueOf(e.Description).trim().replaceAll('\s+',' ').
\s+ finds all continuing spaces.

  • Try to avoid posting additional answers with follow up. Instead, use the checkbox underneath the answer which was the most helpful to you to mark the best answer. Commented Apr 22, 2016 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .