Convert String to int Array

Is there any way to convert String s = “1234” to int s = {1,2,3,4}

Thank you.

String s = "1234";
int[] intArray = new int[s.length()];

for (int i = 0; i < s.length(); i++) {
	intArray[i] = Character.digit(s.charAt(i), 10);

Thank a lot!:wink:

simple and best answer… ,if performance is matters, better use For-Each loop instead of for loop…

how to impllement in for-each loop way?

The for each won’t work since you still need an index. You could use it but since you need the index anyway you might as well just use the for. Although it would look something like this.

String s = "1234";
int[] intArray = new int[s.length()];
char [] asChar = s.toCharArray();
int i=0;
for (char c : asChar) {
 intArray[i++] = Character.digit(c,10);