Welcome to my blog

I have been working with Salesforce for quite a while, so don’t hesitate to contact me if you have any questions or want some advice.

s f

Subscribe
Follow Us
h

STRING SPLIT WITH MULTIPLE SPECIAL CHARATER IN APEX

 . Apex  . STRING SPLIT WITH MULTIPLE SPECIAL CHARATER IN APEX

STRING SPLIT WITH MULTIPLE SPECIAL CHARATER 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);
   }
}

Post a Comment