I have the following code:-

string clientNumber = '019224';
List<Integer> integerList = new List<Integer>();

How do I iterate through the clientNumber string as if it was an array and load each value into integerList to obtain the following values?

integerList[0] = 0
integerList[1] = 1
integerList[2] = 9
integerList[3] = 2
integerList[4] = 2
integerList[5] = 4

5 Answers 5


You can use String split in combination with Integer valueOf: Also, extra checks may be added to ensure that each character is valid digit (thanks to @Novarg)

String clientNumber = '019224';
List<Integer> integerList = new List<Integer>();

List<String> client_digits = clientNumber.split('');
for(String s : client_digits) {
    Integer parsed_digit = safeParse(s);
    if (s == null) {
        // invalid character. Do something with it
    } else {

//Static Utility method
public static Integer safeParse(String input) {
    Integer result = null;
    try {
        result = Integer.valueOf(input);
    } catch (Exception ex) {
        // Log here if there is generic logging utility
    return result;
  • -1, no error management. This will throw an unhandled exception if an initial string contains any non-numerical character
    – Novarg
    Commented Apr 5, 2017 at 8:13
  • I like the updated version more :) Changed to +1
    – Novarg
    Commented Apr 5, 2017 at 8:30

oh well, I thought I'll add another possible solution. You can simply use regex and get out all non-numeric characters and then just split and cast it:

String testString = 'h3ll0 w0r1d';
String numericString = testString.replaceAll('[^0-9]', '');
List<String> stringList = numericString.split('');
List<Integer> integerList = new List<Integer>();
for (String s : stringList) {

Thanks to kurunve for pointing out my mistake in the original post

  • Unfortunately, it is impossible to case List<String> to List<Integer> by implicit casting. Unless that, it is a nice way to get digits from input string
    – kurunve
    Commented Apr 5, 2017 at 8:50

You might consider the Pattern and Matcher classes here:

public static List<Integer> getDigits(String input)
    List<Integer> digits = new List<Integer>();
    Matcher m = Pattern.compile('\\d').matcher(input);
    while (m.find()) digits.add(Integer.valueOf(m.group()));
    return digits;

It's concise, simple, and avoids any need at all for error handling. I expect it actually performs pretty well comparatively, but I haven't profiled any of these approaches just yet.

Here are a couple test cases just via Execute Anonymous:



(0, 1, 9, 2, 2, 4)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)


You could use different String methods for that.

String clientNumber = '019224';
List<Integer> integerList = new List<Integer>();

String[] chars = clientNumber.split('');
for(String c : chars){
System.debug('<<integerList>> '+integerList);

14:03:25:004 USER_DEBUG [10]|DEBUG|<<integerList>> (0, 1, 9, 2, 2, 4)


convert string to integer. your answer here with example .

Please go through this link:


let me know if this help you.

Happy to help You!

Thanks, Anshul sahu

  • 1
    I think when I came across this answer a couple of days ago, I added a comment like this: "What does your linked blog contribute to the already existing, instructive answers to this 5 year old question? This appears to me as yet another prime example of unconvincing self advertisement." I think Moonpie at the time expressed similar criticism. And then you deleted your answer - just to repost it again! What sense does this make? Commented Sep 29, 2022 at 7:25
  • 1
    @FelixvanHove : Yes, this is déjà vu! Anshul sahu: SFSE welcomes contributions, and does not mind some self-promotion, but it needs to be within reason, within the rules & guidelines, should only be portion of your posts. If you keep posting low quality posts that the community legitimately downvotes you could be disallowed from contributing. Please take The Tour and read the Help Center.
    – Moonpie
    Commented Sep 29, 2022 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .