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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With