1

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

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 {
        integerList.add(parsed_digit);
    }
}

//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 Apr 5 '17 at 8:13
  • I like the updated version more :) Changed to +1 – Novarg Apr 5 '17 at 8:30
3

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) {
    integerList.add(Integer.valueOf(s));
}

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 Apr 5 '17 at 8:50
3

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:

system.debug(getDigits('019224'));
system.debug(getDigits('123-456-7890'));

Yields:

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

1

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){
    if(c.isNumeric()){
        integerList.add(Integer.valueOf(c));
    }
}
System.debug('<<integerList>> '+integerList);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.