In original Contacts, Tim Barr for example, I see that his address is written in the Street field like this :

2335 N. Michigan Avenue, Suite 1500
Chicago, IL 60601, USA

I tried to remove the new line of this textArea with several differents replace methods but nothing of them works :

street.replace('\n', ' ');
street.replace('\\n', ' ');
street.replace('\r\n', ' ');
street.replace('\\r\\n', ' ');

What's the correct syntax ?

3 Answers 3


It depends on which character is used (that differs between operating systems). If you use these three (make sure the \r\n one is first) it should work:

street = street.replace('\r\n', ' ');
street = street.replace('\n', ' ');
street = street.replace('\r', ' ');
  • You mean I need to call this three methods on a row ? I used the methods I listed in my post one by one. I'll try.
    – SF_user
    Aug 8, 2014 at 11:13
  • 1
    If you run them all (with \r\n first) you're sure every possibility will be replaced. You can also use the replaceAll() function, that gets a regular expression as a first argument, so you can reduce this to one line. (see salesforce.com/us/developer/docs/dbcom_apex250/Content/…) Aug 8, 2014 at 11:16

Same answer as rael_kid, but in one regex:

replaceAll('\r\n|\n|\r',' ');

I dont know previously it was supported or not, but now best way to achieve it is:

street = street.unescapeJava();

You must log in to answer this question.

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