Apex – Convert comma-separated String to a List

How to convert below String into List<String> in Apex Programming ?

String alpha = 'A, B, C, D';

In java, its very easy like below:

List<String> result = Arrays.asList(alpha.split("\\s*,\\s*"));

Answer

Salesforce provide String class which can be used to work with string. In your case you can use Split method

String alpha = 'A, B, C, D';

List<String> lstAlpha = alpha.split(',');

System.debug(lstAlpha);

Attribution
Source : Link , Question Author : user4567570 , Answer Author : Himanshu

Leave a Comment