Apex · Salesforce

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);
   }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s