Trying to replace a character '+' with underscore. Tried:
String s = 'Happy+Me'; System.debug(s.replaceAll('\\+', '_'); System.debug(s.replaceAll('/\[+]', '_');
Still gives me 'Happy+Me' result. Well it doesn't seem to work.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
replace instead of
Use the appropriate regular expression for
replaceAll. Note that regular expressions use
\ to escape, but Apex also uses
\ to escape. This means you need to use twice as many
\ to get the correct result:
To match a literal
\ in a regular expression string, you would need to use four
If you're using a Custom Label or other source for your regular expression string, you would not need to escape the
\, as this only applies during the compilation of Apex code.