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

(7179)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 13x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply