· development · 1 min read

Salesforce Apex String Replace Method Explained

How to replace substrings within a string with the Apex String by using the replace, replaceAll and replaceFirst method. You will get to know each method with examples.

Learn how to replace substrings within a string with the Apex String replace, replaceAll & replaceFirst method. [Code Examples]

The Apex String class offers three replace methods:

  • replace - most common, you provide what should be replaced by what
  • replaceAll - with replaceAll you can define a regular expression where all substrings that matches the regular expression are substituted.
  • replaceFirst - is basically the same as replaceAll, but only the first match gets replaced

Apex String Replace Method

Let’s start by taking a look at the details of the replace method of the String class.

<String>.replace
String replace(String target, String replacement))
Replaces all occurrences of the target string with the replacement string within this string.
Input parameter
  • target (String): The string that should be replaced.
  • replacement (String): The replacement string of the given target string.
Return value
  • String: A string where all the occurrences of target are replaced by the replacement string.
Example
String fruits = 'apple banana orange';
String fruitsCsv = fruits.replace(' ',',');
System.assertEquals('apple,banana,orange', fruitsCsv);

Examples - Apex String replace method

Remove spaces

To remove all whitespaces of a string, you could use the replace method to replace the spaces by nothing.

String abc = 'a , b , c  ';
String result = abc.replace(' ','');
System.assertEquals('a,b,c',result);

ReplaceAll Method

The replaceAll method takes a regular expression to replace all matching strings.

<String>.replaceAll
String replaceAll(String regExp, String replacement))
Replaces all matches of the given regular expression with the replacement string within this string.
Input parameter
  • regExp (String): The regular expression to match substrings within the string that should be replaced.
  • replacement (String): The replacement string to replace matches of the given regular expression.
Return value
  • String: A string where all the matches of the regular expression are replaced by the replacement string.
Example
String result = '123-456-789'.replaceAll('[\\d]','+');
System.assertEquals('+++-+++-+++',result);

ReplaceFirst Method

The replaceFirst method replace only the first match of the given regular expression.

<String>.replaceFirst
String replaceFirst(String regExp, String replacement))
Replaces the first match of the given regular expression with the replacement string within this string.
Input parameter
  • regExp (String): The regular expression to match a substring within the string that should be replaced.
  • replacement (String): The replacement string to replace matches of the given regular expression.
Return value
  • String: A string where the first match of the regular expression is replaced by the replacement string.
Example
String result = '123-456-789'.replaceFirst('[\\d]','+');
System.assertEquals('+23-456-789',result);
Share:
Back to Blog