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.
- 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.’
- 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.

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.
- 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);- 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.

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() function, which basically removes them from the string.
Now, follow the steps below to replace special characters from a string in Salesforce Apex.
- 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);- 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.

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.
- 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);- 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.

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.
- 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);- 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.

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.
- Find the Length of the String Using Apex in Salesforce
- Truncate String in Salesforce Apex
- Check if a String is Null, Empty or Blank in Salesforce Apex
- Split String Method in Salesforce Apex
- Remove First and Last Character from a String in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.