If the original string contains supplementary Unicode characters, then split() would not work, as it splits these characters into surrogate pairs. To correctly handle these special characters, a code like this works:
String[] chars = new String[stringToSplit.codePointCount(0, stringToSplit.length())];
for (int i = 0, j = 0; i < stringToSplit.length(); j++) {
int cp = stringToSplit.codePointAt(i);
char c[] = Character.toChars(cp);
chars[j] = new String(c);
i += Character.charCount(cp);
}
Maybe you can use a for loop that goes through the String content and extract characters by characters using the
charAt
method.Combined with an
ArrayList<String>
for example you can get your array of individual characters.If characters beyond Basic Multilingual Plane are expected on input (some CJK characters, new emoji...), approaches such as
"a
Take a look at the String class's getChars() method.
If the original string contains supplementary Unicode characters, then
split()
would not work, as it splits these characters into surrogate pairs. To correctly handle these special characters, a code like this works:To sum up the other answers...
This works on all Java versions:
This only works on Java 8 and up: