Splitting String Example in Salesforce Apex Class:

Returns a list that contains each substring of the String that is terminated by the regular expression regExp, or the end of the String

In the following example, a string is split, using a dot as a delimiter:

Ex:1
string s = ‘first.second’;
string[] part;
system.debug(part.size());
part = s.split(‘\\.’);

Note: if you are spliting the string using special character like (.,*,etc) then use a backslash before the special character.

In the following example, a string is split, using a * character as a delimiter:

Ex:2
string sString = ‘theblogreaders*salesforce’;
string[] splitted = sString.split(‘\\*’);
system.debug(‘part1:: = ‘ + splitted[0]);   //Output: theblogreaders
system.debug(‘part1:: = ‘ + splitted[1]);   //Output: salesforce

Written by 

Salesforce MVP | Lightning Champion | 16x Salesforce Certified | Application Architect | 6x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*