While working as a Salesforce developer, I developed a feature that allows users to enter multiple email addresses separated by commas into a custom field.
There, I had to extract and validate each email address individually. To separate the emails, I have used the split() function to extract the emails from the string field.
In this Salesforce tutorial, I will explain several approaches for how to split string values in Salesforce Apex.
Split Strings in Salesforce Apex
To split the strings in a field, follow the steps below.
- Navigate to the Salesforce developer console and execute the anonymous window with “Ctrl+E”. Enter the code below in the developer console.
String emailList = 'Eva.James@mail.com, nathan.coulter@mail.com, mike.smith@mail.com';
if (emailList != null && emailList != '') {
String[] emails = emailList.split(',');
for (String email : emails) {
email = email.trim();
if (isValidEmail(email)) {
System.debug('Valid Email: ' + email);
} else {
System.debug('Invalid Email: ' + email);
}
}
} else {
System.debug('No emails found.');
}
public Boolean isValidEmail(String email) {
String emailRegex = '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$';
Pattern pattern = Pattern.compile(emailRegex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}- After entering the above code, select the checkbox Open in debug and click on the Execute button.
- In the Execution log window, select the checkbox to debug only, and then you will see the split string values in the debug console.

Following the above steps, we can split the string into a list using the split method in Salesforce Apex.
Split and Trim String in Salesforce Apex
To split a string and eliminate any leading or trailing whitespace, follow the steps below to split and trim a string in Salesforce Apex.
- Navigate to the developer console and execute the anonymous code window. Enter the code below in the anonymous code window, then click on the Execute button.
String Users = 'Steve,Ema,James';
String[] result = Users.trim().split('\\s*,\\s*');
for (String user : result) {
System.debug(user);
}- In the console log window, select the checkbox to debug only, and you will see the string split and trimmed in debug.

In this way, we can split and trim a string in Salesforce Apex by following the above code.
Split a String by Dot in Salesforce Apex
In an Apex string, the dot has a meaning within regular expressions, so to remove the dot, we need to escape it with two backslashes.
- Navigate to the developer console and execute the anonymous window. Enter the code below in the anonymous window, then click the Execute button.
String Users = 'Smith.John.Andrew';
String[] result = Users.split('\\.');
System.debug(result);
- In the Console log window, you will see that the string with a dot is split into three string values.

This is how we can split a string value joined by a string using the str.split(‘\\.’) method.
Split String by Character in Salesforce Apex
Using the split method, we can also split a string by character, but remember that the character in the string is case-sensitive.
- Navigate to the Salesforce developer console and execute the anonymous window. Enter the code below in the anonymous window and execute it.
String Users = 'SmithXJohnXAndrew';
String[] result = Users.split('X');
System.debug(result);The output of the above code will be:
['Smith', 'John', 'Andrew']- In the debug, you will see the following output of the string values.

Using the above apex code, you can easily split a string by character. Since the character here is case-sensitive, check the uppercase and lowercase of the character in the code.
Split String by Space in Salesforce Apex
We have to pass the white space through the split method to split a string by space. Follow the steps below to split the string by space.
- Navigate to the Salesforce developer console and execute the anonymous window. Enter the code below in the anonymous window and execute it.
String Users = 'Edward Rayn Stuart';
String[] result = Users.split(' ');
System.debug(result);- The output of the above code in list format will be as:
('Edward', 'Rayn', 'Stuart')- The debug will show output in the console log as an array of split string values.

Using the above method, we can split a string list by space in Salesforce Apex.
Split String by New Line in Salesforce Apex
To split a string by a line break, we will use the ‘\n’ as an alias for the new line.
- Navigate to the Salesforce developer console and execute the anonymous window. Enter the code below in the anonymous window and execute it.
String Users = 'Ama\nSteve\nBrayan';
String[] result = Users.split('\n');
System.debug(result);
- The output of the above code will be:
('Ama', 'Steve, 'Brayan')- The split string values will appear in the debug console, as shown below.

This way, we can split a string by the new line character ‘\n’ in Salesforce Apex with the above code.
Split a String by Pipeline in Salesforce Apex
In my Apex code, I tried to split the string “Apex | Onsite | Sponsorship | Editorial” using the split method. In my case, the str.split(‘|’) method did not work. It was giving output like ‘A,p,e,x’.
Then, I took a different approach and used str.split(‘\\|’) and got the desired output.
- Navigate to the Salesforce developer console and execute the anonymous window. Enter the code below in the anonymous window and click the execute button.

- In the debug of the console log window, you will see the string values split from the pipeline.

This way, we can split a string from a pipeline in Salesforce apec using the str.split(‘\\|’) method.
Conclusion
In this Salesforce tutorial, we have learned the different approaches to splitting a string in Salesforce Apex. We have included the most common split by “comma(,)” and discussed other split methods like split by dot, Character, New Line, Space and Pipeline, through which we can split the string using the split() function in Salesforce Apex.
You may also like to read.
- Convert String to Blob in Salesforce Apex
- Convert String To Lowercase And Uppercase In Salesforce Apex
- Convert a String to Decimal in Salesforce Apex
- Convert String to Boolean 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.