How to Replace String in Salesforce Apex

While working as a Salesforce admin for an organization that tracks leads from multiple sources, I noticed inconsistencies in the Lead Source data. Instead of the web, it was entered as a website, so I had to replace the current lead source value with the web.

To do this, I used the built-in Salesforce Apex function replace() to replace the old string value with the new one. In this Salesforce tutorial, I will explain how to replace a string value in Salesforce Apex.

Syntax to replace a string in Salesforce Apex

Using the replace() function, the syntax for replacing a string in Apex is as follows.

String newString = originalString.replace(oldStringValue, newStringValue);

Replace String Value in Salesforce Apex

To replace a string value in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.

  1. In the Salesforce developer console, open the anonymous Apex code window with “Ctrl+E.
    • Enter the code below in the anonymous apex window, and click the Execute button.
String originalString = 'Lead Source - Website';
String replacedString = originalString.replace('Website', 'Web');
System.debug(replacedString);

In the above code, ‘Website’ is the current substring value that is to be replaced with the substring value ‘Web.’

  1. In the execution log window, select the checkbox to debug only. After this, you will see the replaced value of the substring in the debug.
Replace string in Salesforce Apex

This way, we can replace a string value in Salesforce Apex using the replace() function.

Replace All Similar String Values in Salesforce Apex

In Salesforce Apex, the replaceAll() method replaces all substring occurrences that match a specified regular expression with another string.

Follow the steps below to replace all occurrences of a substring that match an expression with another string.

  1. Navigate to the developer console and launch the anonymous apex code window with the shortcut keys “Ctrl+E.”
    • Enter the code below in the anonymous window and click the Execute button.
String originalString = 'Lead Source - Website, Referral - Website, Marketing Campaign - Website';
String replacedString = originalString.replaceAll('Website', 'Web');
System.debug(replacedString);
  1. In the execution log window, select the checkbox to debug only. After this, in the debug, you will see the substring is replaced with the new string value.
Replace all strings in Salesforce Apex

This way, we can use the replaceAll() method to replace all substring occurrences that match an expression with another string in Salesforce Apex.

Replace the Special Character from the String in Apex

In Apex, we can remove special characters from the string using the replaceAll() functionwhich basically removes them from the string.

Now, follow the steps below to replace special characters from a string in Salesforce Apex.

  1. Navigate to the Salesforce developer console and launch the anonymous code window with “Ctrl+E.”
    • Enter the code below in the anonymous window and click on the Execute window.
String originalString = 'Salesforce_development@Apex*#2024!';
String cleanedString = originalString.replaceAll('[^a-zA-Z0-9]', ''); 
System.debug('Cleaned String: ' + cleanedString);
  1. In the execution log window, select the checkbox to debug only. After this, in the debug, you will see that the special characters are removed from the string.
Replace special characters from string in Salesforce Apex

In this way, we can replace the special characters from a string by following the above steps.

Replace Multiple Characters of a String at the Same Time in Salesforce Apex

In Salesforce, we can use the replace() or replaceAll() in multiple steps to replace multiple substrings within a string.

Follow the steps below to replace multiple characters in a string in Salesforce Apex at the same time.

  1. In the Salesforce developer console, launch the anonymous Apex code window with “Ctrl+E.”
    • Enter the code below in the anonymous code window and click the Execute button.
String sentence = 'Apex used in Salesforce. LWC is Salesforce feature.';
sentence = sentence.replace('Apex', 'Java').replace('LWC', 'Aura');
System.debug(sentence);
  1. In the execution log window, select the checkbox to debug only; then, in the debug, you will see the replaced values of the substrings in the string.
How to replace two characters of a String in Salesforce apex

This is how we can replace multiple substrings in a string in Salesforce Apex.

Replace HTML Characters from a String in Salesforce Apex

In Salesforce Apex, we can replace the HTML characters from a string using the replaceAll() function.

Follow the steps below to learn how to replace the HTML characters from a string in Salesforce Apex.

  1. Navigate to the Salesforce developer console and launch an anonymous Apex window.
    • Enter the code below in the anonymous window, and click Execute.
String content = 'Send email to {!<p class="main">Name}</p>';
content = content.replaceAll('<[^>]*>', '');
System.debug(content);
  1. In the execution log window, select the checkbox to debug only. Then, in the output, you will see that the HTML characters are replaced in the string.
Replace html characters from string in Salesforce apex

This way, replace HTML characters from a String in Salesforce Apex by following the above steps.

Conclusion

In this Salesforce tutorial, we learned how to replace string values in Salesforce Apex. In the above steps, we discussed multiple scenarios for replacing a string in Salesforce Apex. We have discussed replacing substrings for single strings, similar string values, strings with special characters, multiple characters in strings, and HTML characters from strings.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.