There is a String value Eg:('12560') and I need to put it into an array by index wise.
Eg : Input:
String x = "12560";
Output:
String arr[] = {"1","2","5","6","0"};
There is a String value Eg:('12560') and I need to put it into an array by index wise.
Eg : Input:
String x = "12560";
Output:
String arr[] = {"1","2","5","6","0"};
Here's a one line answer:
JAVA 8 and above:
x.split("");
JAVA 7 and below:
x.split("(?!^)");
This should do the job:
String x = "12560"
char[] chars = x.toCharArray();
Something like this shall work.
Create a new String [] array, proceed with adding each character as a new string inside it.
String x = "12560";
String output [] = new String[x.length()];
for(int i = 0 ; i < x.length() ; i++){
output[i] = x.charAt(i)+"";
System.out.println(output[i]);
}
Output -
1
2
5
6
0
This code is worked for me.
//c is the user input value
String c = "12560";
//create a String array to add final results
String finArr[] = new String[c.split("").length];
//put input value to an array by splitting
String arr[] = c.split("");
//add
for(int i=0;i<arr.length;i++){
finArr[i] = arr[i];
}
for(int j=0;j<finArr.length;j++){
System.out.println(finArr[j]);
}