Latest Updates

Travel to Technology

Dayton Metro Library

String split with multiple special character in apex

Just experienced issue to split the string value, one of API integration where JSON string attribute contains the many special character that I need to split and reorganize the value then store the value in salesforce field.

Example:

JSON Response

{
"To": "Test User <test.user@test.com>",
"Cc": "Test User1 <test.user1@test.com,Test User2 <test.user2@test.com;Test User3 <test.user3@test.com;Test User4 <test.user4@test.com"
}

Split the string after you parsed JSON string and stored the value in String variable – you can use regular expression in string split method

String strEmailCC = objREsponse.cc;
list<String> lstEmailString = strEmailCC.split('[;,]');

Apex code to remove the name from email Address:

for(String strEmail : lstEmailString) {
   if(strEmail.endsWith('>')) {
        strEmail = strEmail.substring(strEmail.indexOf('<', 1)+1, strEmail.length()-1);
   }
}