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
asadimasadim 

String repalce method doesn't seem to work properly

The replacement does not happen at all in this code. Any ideas why?!!

 

 

String res = //some very long string of length 72000 chars
res = res.substring(4000, 5000); //this substring contains carriage return chars \n

res = res.replaceAll('\n', ' ');
system.debug(res);

 

Thanks in advanace!

Best Answer chosen by Admin (Salesforce Developers) 
imuino2imuino2

Are you sure the \n isn't being escaped when you save it, or query it. It could be changed for <br> at some moment.

I did test your code an it worked well, so i think the problem is that the \n is being replaced on some instance before.

 

Ignacio.

All Answers

iBr0theriBr0ther

What I can suggest:

 

Use Encodingutil.urlEncode(str, 'UTF-8');

 

'\n' shall be converted to '%0D%0A'

 

Replace it and than decode it.

 

Hope this help

 

Cheers,

imuino2imuino2

Are you sure the \n isn't being escaped when you save it, or query it. It could be changed for <br> at some moment.

I did test your code an it worked well, so i think the problem is that the \n is being replaced on some instance before.

 

Ignacio.

This was selected as the best answer
asadimasadim

If you type in the string manually it works. But if the initial string is coming from somewhere else it doesn't. In my case the string was coming from another server as a JSON string. I still don't understand what was going on. I fixed it using JavaScript eventually.