How to Split the special characters using apex class

Its possible to split the string using the characters/letters using the SPLIT method using apex class.
The separator string used in the split method is a regular expression and “.” is a special character in regular expressions.

The regular expression for a literal “.” is “\.”
However “\” is also used to escape characters when expressing Strings in Apex, and so this character too needs escaping:
Splitting a string using ‘.’
String[] d = createDate.split(‘\\.’);
system.debug(d[0]);
system.debug(d[1]);

Same way we can split the other special characters like @,&,$,!,etc

Splitting a string using ‘|’
| ====> ‘|’
String[] d = createDate.split(‘\\|’);
system.debug(d[0]);
system.debug(d[1]);

Splitting a string using ‘\’

The documentation provides an interesting example, if you need to use “\” as the separator:

List<String> parts = filename.split('\\\\');

(888)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 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