how could I convert my String to int array? my input:
String numbers = "123456";
What I'd like to reach:
Int numbers[]={1,2,3,4,5,6};
That String was splitted from String with this number.
how could I convert my String to int array? my input:
String numbers = "123456";
What I'd like to reach:
Int numbers[]={1,2,3,4,5,6};
That String was splitted from String with this number.
If the above question is for Java.
String numbers = "123456";
int[] array = new int[numbers.length()];
for (int i = 0; i < numbers.length(); i++) {
array[i] = Character.getNumericValue(numbers.charAt(i));
System.out.println("\n"+array[i]);
}
Java 8 one liner would be:
int[] integers = Stream.of( numbers.split("") )
.mapToInt(Integer::valueOf)
.toArray();
I don't know what language you are working with. But I can tell you in c++.
character codes of digits start from 48(dec). You can add and remove this value for each element.
The code could roughly look like this.
int * _numbers=new int[numbers.size()];
for(int i=0;i<numbers.size();i++)
_numbers[i]=numbers[i]+48;
The most straightforward way is to create an array variable and loop through the string characters and push them into the array.
var str = "123456";
var arr = [];
for (let n of str) {
arr.push(parseInt(n))
}
console.log(arr);