function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nandhini S 3Nandhini S 3 

How to split values in a field in a list

Hi Guys,

I have a field which has values separated by commas. I have queried this field and stored in a list. The list has only one record. How do I separate the values in the field?
ankit bansalankit bansal
Hi Nandhini,
Please use the split method on the string - https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_System_String_split

So if  your
String str =  'abc,def';
str.split(',')  wil result in ['abc','def'] a list of the values.
Suraj Tripathi 47Suraj Tripathi 47
Hi Nandhini S 3,

String addressFull  = 'F-206, Daffodils, Magarpatta, Pune, India, 411028';
String[] address     = addressFull.split(',');
String houseNumber         =     address[0]; 
String buildingName          =    address[1];
String streetName             =    address[2];
String cityName                 =    address[3];
String countryName           =    address[4];
System.debug(houseNumber);
System.debug(buildingName);
System.debug(streetName);
System.debug(cityName);
System.debug(countryName);

Also please refer to this link:
https://www.infallibletechie.com/2020/03/string-split-method-in-apex-in.html

Please mark it as the best answer, if it helps in any way.

Thanks!