9

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];
2
  • do you mean array of char arrays? Commented Jul 12, 2011 at 21:39
  • 1
    string.toCharArray() and new String(chararray) Commented Jul 12, 2011 at 21:42

6 Answers 6

21

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("(?!^)");
Sign up to request clarification or add additional context in comments.

3 Comments

you actually get a bit of weirdness if you do .split("") as there an empty string at the beginning of the string (or it's something like that) either way the output has a empty zero length string in position 0 myarray
Well yes, but if that's the case, one could just create another array without the first element.
Thanks, this works but does add the extra empty character at the beginning. I can work with this though
7

Beautiful Java 8 one-liner for people in the future:

String[] array = Stream.of(charArray).map(String::valueOf).toArray(String[]::new);

4 Comments

won't work for me, I'm getting an ArrayStoreException
@kopaka that‘s weird, it worked for me. Could you show me your code?
char [] regKey = {'A','B','C','D'}; String[] array = Stream.of(regKey).toArray(String[]::new); leads to an ArrayStoreException
This isn't a good solution, it merges all of the characters into one string.
3

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.

2 Comments

how can I remove "" fro the array. I need only {"a", "b", "c", "d", "e", "f" }. Any possibility?
@CHAKRAVARTHI This is not a dynamic array, so you have to create a new array with the slots you would like to keep. This can be done plenty of ways. If it is just a matter of skipping the first "", it is maybe better to index the array a 1-based way instead of a 0-based way.
2

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);
}

Comments

0

This will give string[] as result, but will have only one value as below String[] str = {"abcdef"};

Comments

0

// char[] to String[] String[] sa1 = String.valueOf(cArray).split("");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.