+ Start a Discussion
CMcCaulCMcCaul 

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?
 
Thanks.


Message Edited by CMcCaul on 09-17-2008 11:45 AM
mikefmikef
This is a good resource for regExp, http://www.regular-expressions.info/
prageethprageeth

Try this

 

 

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

SennahSennah

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.

 

Hth,

//Hannes

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