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.
The above answers are more straightforward ways to do achieve this; however, you can also take the substring of the array between a value of i and i+1, parse that string as an integer, and then assign the array at index i to the value of the integer to achieve the same goal. Here is the code below:
public class StringtoIntArray {
public static void main(String[] args) throws Exception
{
String numbers = "123456";
int[] array = new int[numbers.length()];
String temp;
int holder;
for(int i=0;i<numbers.length();i++)
{
temp=numbers.substring(i,i+1);
holder = Integer.parseInt(temp);
array[i]=holder;
System.out.println(array[i]);
}
}
}
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;