9 votes
Accepted

How to remove ( from the string

Something like : String s = '(09890)'; Integer i = Integer.valueOf(s.replaceAll('[\\(\\)]',''));
user avatar
  • 5,478
9 votes
Accepted

How to replace 'MyFirstName' by 'My First Name'

I will answer to my own question : String text = 'ILikeSalesforcePlatform'; text = String.valueOf(text.splitByCharacterTypeCamelCase()).replaceAll('[\\,\\(\\)]',''); System.debug(text); Output : ...
user avatar
  • 5,478
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');
user avatar
  • 429k
5 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 (-).
user avatar
  • 429k
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: ...
user avatar
  • 4,654
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 [^\...
user avatar
  • 429k
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 ...
user avatar
  • 129k
4 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' ...
user avatar
  • 85.9k
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....
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: {"...
user avatar
  • 53.1k
2 votes
Accepted

Translate spanish characters to english for field values

The replace() function does not edit the variable you put in front of it but returns a new String. Please replace: str.replace(key,M.get(key));//Get CORR Value with: str = str.replace(key,M.get(key)...
user avatar
2 votes

Issue with the string.replace method in salesforce

You may be able to leverage the String.format() method to do parameter substitution rather than using the String.replace() method and hoping for the best. Parameter substitution should be a much safer ...
user avatar
  • 22.3k
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(...
user avatar
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 ...
user avatar
  • 129k
1 vote

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 ...
user avatar
  • 429k
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 ...
user avatar
  • 429k
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, ...
user avatar
  • 46.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 ...
user avatar
  • 429k
1 vote
Accepted

Replace + Sign by Underscore

you have not close bracket. use this below code String s = 'Happy+Me'; System.debug(s.replaceAll('\\+', '_'));
user avatar
  • 1,298
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....
user avatar
1 vote
Accepted

Issue with the string.replace method in salesforce

This isn't 100% foolproof depending on the user input, but by doing an initial replacement on the keys with values that won't exsist in the replacement data you can make the problem a lot easier: ...
user avatar
  • 24.7k

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