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

Help with ReplaceAll()

I am Java/Apex rookie and I am trying to strip cr/lf characters from a string using the ReplaceAll method.  c.ReplaceAll('\r\n','') doesn't work I guess because '\r\n' isn't a regExp.  I'm sure there's a simple solution but I haven't found it yet.  Can anyone offer some help?

Message Edited by CMcCaul on 09-17-2008 11:45 AM
This is a good resource for regExp,

Try this



return stringWithLineBreaks.replaceAll('\n',''); 


I had some trouble with the replaceAll() function too. After solving my problem I thought it might be helpful to add my solution here, as it is one of the first Google hits.


To use a regular expression in the string function replaceAll() follow this code example.



Pattern nonWordChar = Pattern.compile('[^\\w]');
sw = nonWordChar.matcher(sw).replaceAll('');



In this example I am replacing all non-word characters (which means everything but letters and numbers) with nothing.

sw is a String.


Pretty strange to go this way, but at least it works :) Kudos to this blog via which I found the solution.




@Sennah Thanks so much for sharing! Very helpful :)
Ryan BoosRyan Boos
Perfect advice. Thank you! This worked easily by simply changing the regex :)