How do you convert a char array to a string array? Or better yet, can you take a string and convert it to a string array that contains each character of the string?
Edit: thanks @Emiam: Used his code as a temp array then used another array to get rid of the extra space and it works perfectly:
String[] tempStrings = Ext.split("");
String[] mStrings = new String[Ext.length()];
for (int i = 0; i < Ext.length(); i++)
mStrings[i] = tempStrings[i + 1];
Or better yet, can you take a string and convert it to a string array that contains each character of the string?
I think this can be done by splitting the string at "". Like this:
String [] myarray = mystring.split("");
Edit: In case you don't want the leading empty string, you would use the regex: "(?!^)"
String [] mySecondArray = mystring.split("(?!^)");
Beautiful Java 8 one-liner for people in the future:
String[] array = Stream.of(charArray).map(String::valueOf).toArray(String[]::new);
I have made the following test to check Emiam's assumption:
public static void main(String[] args) {
String str = "abcdef";
String [] array = str.split("");
}
It works, but it adds an empty string in position 0 of the array. So array is 7 characters long and is { "", "a", "b", "c", "d", "e", "f" }.
I have made this test with Java SE 1.6.
brute force:
String input = "yourstring";
int len = input.length();
String [] result = new String[len];
for(int i = 0; i < len ; i ++ ){
result[i] = input.substring(i,i+1);
}
This will give string[] as result, but will have only one value as below String[] str = {"abcdef"};
// char[] to String[] String[] sa1 = String.valueOf(cArray).split("");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.