In Apex code how do we convert string to array. Java toCharArray does not work.
String string = 'somestring' to [s, o, m, e ...]
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
You can use the String.split() method, using
'' (two single quotes) as a parameter::
String ourString = 'Salesforce SE'; String stringCharacters = ourString.split(''); System.debug(stringCharacters); // stringCharacters is a List<String>, each element being a substring of length 1: // (,S,a,l,e,s,f,o,r,c,e, ,S,E)
Note that the first element in the collection will be "empty":
String firstElement = stringCharacters.remove(0); System.debug(stringCharacters); // (S,a,l,e,s,f,o,r,c,e, ,S,E)