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'
...
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');
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 (-).
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: ...
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 [^\...
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 ...
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 ...
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 ...
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....
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(...
2
votes
Simple APEX String Replacement - can't unencode quotation marks
Unencode the body before deserializing.
String t = '{"meta":{"reuest":{"params"}}';
t = t.unescapeHtml4();
system.debug(t);
Results in:
{"...
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 ...
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 ...
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), ...
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.
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 ...
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, ...
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 ...
1
vote
Accepted
Replace + Sign by Underscore
you have not close bracket. use this below code
String s = 'Happy+Me';
System.debug(s.replaceAll('\\+', '_'));
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.
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....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
replaceall × 24string × 14
apex × 13
regular-expressions × 8
replace × 6
json × 4
api × 2
pattern × 2
soql × 1
query × 1
controller × 1
class × 1
data-loader × 1
map × 1
invocable-method × 1
richtextarea × 1
array × 1
component × 1
soql-injection × 1
substring × 1
escape × 1
key × 1