I have an array of type char which has some characters stored in it. I want to take each individual character and store it in a string. How do i do that?
Asked
Active
Viewed 145 times
-1
-
I don't want to store all the elements in a single string. I want to store each character in a different string – user3526197 Oct 31 '14 at 14:10
-
1Do this (http://stackoverflow.com/questions/8172420/how-to-convert-a-char-to-a-string-in-java) for each element of your array – Jonny Oct 31 '14 at 14:11
-
for (char c : myCharAry){ String tmpCharacterAsString = new String(c); } – JBA Oct 31 '14 at 14:11
3 Answers
1
public String[] charArrayToStringArray(char[] c){
String[] s = new String[c.length];
for(int i = 0; i < c.length; i++){
s[i] = String.valueOf(c[i]);
}
return s;
}
A simpler way would be:
char[] c = new char[]{'a', 'b', 'c', 'd'};
String[] s = new String(c).split("");
It works, but it adds an empty string in position 0 of the array.

Pier-Alexandre Bouchard
- 5,135
- 5
- 37
- 72
0
You can store your strings in another array:
String[] stringArray = new String[charsArray.length];
for (int i=0; i<charsArray.length; i++){
stringArray[i] = "" +charsArray[i];
}

alex.pulver
- 2,107
- 2
- 31
- 31
-1
char[] charArray = {'a', 'b', 'c'};
String str = String.valueOf(charArray);
System.out.println(str);
or
String sTest="";
for (char c : charArray) {
sTest+=c;
}
System.out.println(sTest);

Hiten Chauhan
- 1
- 1