Use this:
String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
Answer from Kuldeep Jain on Stack OverflowUse this:
String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
One liner with java-8:
String str = "testString";
//[t, e, s, t, S, t, r, i, n, g]
Character[] charObjectArray =
str.chars().mapToObj(c -> (char)c).toArray(Character[]::new);
What it does is:
- get an
IntStreamof the characters (you may want to also look atcodePoints()) - map each 'character' value to
Character(you need to cast to actually say that its really achar, and then Java will box it automatically toCharacter) - get the resulting array by calling
toArray()
Videos
String[] sArray = {"i", "love", "you"};
String s = "";
for (String n:sArray)
s+= n;
char[] c = s.toCharArray();
You can do that like this
char[] allchar = new char[total]; // Your code till here is proper
// Copying the contents of the 2d array to a new 1d array
int counter = 0; // Counter as the index of your allChar array
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a1[i].length; j++) { // nested for loop - typical 2d array format
allchar[counter++] = a1[i][j]; // copying it to the new array
}
}
I believe this could be solved using String#split.
String[] lettersOfHello = "hello".split("");
Will need to circle back if it is preinstalled in Java 8 or no introduced until a later package.
Here's a solution that:
- doesn't use any imports – except for
java.util.Arraysso that it can print out the character array for verification, but that's only for this answer, doesn't have anything to do with the code solution itself. - builds an array of Character, and not
java.lang.Stringas you posted in your code – your expected output["h", "e", "l", "l", "o"]could be an array of Strings (double-quotes) but also looks like it would work as characters
import java.util.Arrays;
String s = "hello";
Character[] chars = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
chars[i] = s.charAt(i);
}
System.out.println("chars[]: " + Arrays.toString(chars));
Here's the output:
chars[]: [h, e, l, l, o]
No, that solution is absolutely correct and very minimal.
Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.
Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.
String text = String.copyValueOf(data);
or
String text = String.valueOf(data);
is arguably better (encapsulates the new String call).
To start you off on your assignment, String.split splits strings on a regular expression and this expression may be an empty string:
String[] ary = "abc".split("");
Yields the array:
(java.lang.String[]) [, a, b, c]
Getting rid of the empty 1st entry is left as an exercise for the reader :-)
Note: In Java 8, the empty first element is no longer included.
String strName = "name";
String[] strArray = new String[] {strName};
System.out.println(strArray[0]); //prints "name"
The second line allocates a String array with the length of 1. Note that you don't need to specify a length yourself, such as:
String[] strArray = new String[1];
instead, the length is determined by the number of elements in the initalizer. Using
String[] strArray = new String[] {strName, "name1", "name2"};
creates an array with a length of 3.