How to Split String Method in Salesforce Apex

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.

  1. 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();
}
  1. 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.
How to split string in Salesforce Apex

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.

  1. 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);
}
  1. In the console log window, select the checkbox to debug only, and you will see the string split and trimmed in debug.
Split and trim String in Salesforce apex

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.

  1. 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);
String Split by dot in Salesforce apex
  1. In the Console log window, you will see that the string with a dot is split into three string values.
Split string by dot in Salesforce apex

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.

  1. 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']
  1. In the debug, you will see the following output of the string values.
Salesforce apex string split by character

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.

  1. 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);
  1. The output of the above code in list format will be as:
 ('Edward', 'Rayn', 'Stuart')
  1. The debug will show output in the console log as an array of split string values.
Split String by Space in Salesforce Apex

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.

  1. 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);
Split string by new line in Salesoforce apex
  1. The output of the above code will be:
('Ama', 'Steve, 'Brayan')
  1. The split string values will appear in the debug console, as shown below.
String split by new line in Salesforce apex

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.

  1. Navigate to the Salesforce developer console and execute the anonymous window. Enter the code below in the anonymous window and click the execute button.
Salesforce apex split string by pipeline
  1. In the debug of the console log window, you will see the string values split from the pipeline.
String split by pipeline in Salesforce apex

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.

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.