7 votes
Accepted

Replace single quotes

There is a built-in method String.escapeSingleQuotes() that handles this need nicely. From the documentation, an example: String s = '\'Hello Jason\''; system.debug(s); // Outputs 'Hello Jason' ...
David Reed's user avatar
  • 92.1k
7 votes

replace ' with \' in apex code

There's a method for this: String.escapeSingleQuotes. It's meant to be used to protect against SOQL injection. Identifier = String.escapeSingleQuotes('string\'s length');
sfdcfox's user avatar
  • 481k
6 votes
Accepted

Using String.replaceAll() to remove character combination

You could use replaceAll with a regular expression as follows: url = url.replaceAll('-+','-'); This says "match one or more (+) hyphens (-)" and we replace it with the singular hyphen (-).
sfdcfox's user avatar
  • 481k
5 votes

Replace double character with single in string

You can use the replaceAll() method from the String class. String testString = 'Hello;; my name is SF support.'; testString = testString.replaceAll(';;', ','); System.debug(testString); Result: ...
TSmith's user avatar
  • 4,932
4 votes
Accepted

Replaceall() function working weird

The problem is that you're using two character sets (a set starts with [ and ends with ]). In your first example, the first match happens in the bold area: 7876*78(67)87564#7 Because 6 matches [^\...
sfdcfox's user avatar
  • 481k
4 votes
Accepted

String.replaceAll not able to replace String escape character

These backslashes are usually a sign of unintended double application of JSON serialization: @IsTest private class JsonTest { @IsTest static void test() { Map<String, Object> m ...
Keith C's user avatar
  • 135k
4 votes
Accepted

Regex to find dd/mm/yyyy

There are two issues: replace(String target, String replacement) doesn't take a regExp as input, but a literal string. You should have used replaceAll(String regExp, String replacement) Strings are ...
RubenDG's user avatar
  • 8,239
3 votes

Replace '{}' in Apex

You should use replace(String target, String replacement) instead of replaceAll(String regExp, String replacement): the first replaces each substring of a string that matches the literal target ...
RubenDG's user avatar
  • 8,239
3 votes
Accepted

replace ' with \' in apex code

String str = 'L\'Oreal'; //<-- get the escaped name System.debug(str); // <--debug output: L'Oreal String strEsc = String.escapeSingleQuotes(str); //<-- pass through escape method System....
Umesh Beti's user avatar
2 votes

Regex does not work on incoming mail

Solution: I deleted an extraneous ( which I had in the line in production, but not sandbox and added a +, to make the last section (.+|\\n|\\r) so the final line which has tested OK is .replaceAll(...
user45612's user avatar
2 votes

Simple APEX String Replacement - can't unencode quotation marks

Unencode the body before deserializing. String t = '{&quot;meta&quot;:{&quot;reuest&quot;:{&quot;params&quot;}}'; t = t.unescapeHtml4(); system.debug(t); Results in: {"...
Eric's user avatar
  • 54k
2 votes
Accepted

Removing leading zeroes in Date String

If you really want a locally formatted date, you can use Date.parse and Date.format: String shipDate = '04/06/2022'; System.debug(Date.parse(shipDate).format()); However, be aware that Date.parse ...
sfdcfox's user avatar
  • 481k
2 votes
Accepted

Why can't I apply replaceAll in my controller function?

Your diagnosis is likely correct. To avoid the problem include a null guard: return missingItems != null ? missingItems.replaceAll('Som', '###') : null; The getListOfMissingItems method may be ...
Keith C's user avatar
  • 135k
1 vote

Apex replace \" in string

System.assertEquals(jsonString, 'Error fatal', 'Assertion failed'); Should be: System.assertEquals('Error fatal', jsonString2, 'Assertion failed'); Because strings are immutable (cannot be changed), ...
sfdcfox's user avatar
  • 481k
1 vote

Replace '{}' in Apex

You need to escape the curly braces as follows: .replaceAll('\\{}', 'null'); Such braces are used to group parts of a regex together to express repetition.
TSmith's user avatar
  • 4,932
1 vote

Handling special character in apex

&#x; is a character escape. XML requires this in order to not break protocol when certain characters, such as ', <, and > are included in the text that should be preserved. However, this ...
sfdcfox's user avatar
  • 481k
1 vote

Why can't I apply replaceAll in my controller function?

Your property missingItems is not initialized (and is thus null). You can verify this for yourself by making an instance of your controller in an anoymous apex window (through the developer console, ...
Derek F's user avatar
  • 59.3k
1 vote

Replace + Sign by Underscore

Option 1 Use replace instead of replaceAll: System.debug(s.replace('+','_')); Option 2 Use the appropriate regular expression for replaceAll. Note that regular expressions use \ to escape, but ...
sfdcfox's user avatar
  • 481k
1 vote
Accepted

Replace + Sign by Underscore

you have not close bracket. use this below code String s = 'Happy+Me'; System.debug(s.replaceAll('\\+', '_'));
cloudZigZag's user avatar
  • 1,366
1 vote
Accepted

String Class normalizeSpace() and replaceAll() doesn't work with the Rich text area field

Please, look at the "space" char codes of your textArea string.
Dmitry Kaigorodov's user avatar
1 vote
Accepted

Replace String with Wildcard

The documentation for the replaceAll() method says that it accepts a Regular Expression as the first argument. If you wanted to get rid of everything between | and ! inclusive you can use adder....
Jeff Bennett's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible