5

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 ?

10

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', ' ');
| improve this answer | |
  • 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 '14 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/…) – rael_kid Aug 8 '14 at 11:16
4

Same answer as rael_kid, but in one regex:

replaceAll('\r\n|\n|\r',' ');
| improve this answer | |
2

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

street = street.unescapeJava();

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.