Monday, August 27, 2018

How to split string values using Salesforce Apex Class

How to split string values using Salesforce Apex Class

Split the string values in comma or space or semicolon then use split the below syndax:

List<String> values = splitStringVal.split(',');
List<String> values = splitStringVal.split(';');
List<String> values = splitStringVal.split(' ');


For Example:

String splitStringVal = 'aaa;bbb;ccc;dddd';
List<String> values = splitStringVal.split(';');
for(String value:values){
    System.debug(value +'\n');
}


Output:
aaa
bbb
ccc
ddd


Use String.split(regExp, limit) method:

Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the String.

Example:

String strValue = 'this-is-learn-for-sfdc';
List<String> result = strValue.split('-', 2);
System.debug(result);

Result:
USER_DEBUG [3]|DEBUG|(this, is-learn-for-sfdc)


Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

No comments:

Post a Comment