An efficient way to turn String into an array of single-character strings would be to do this:
String[] res = new String[str.length()]; for (int i = 0; i < str.length(); i++) { res[i] = Character.toString(str.charAt(i)); }
However, this does not take into account the fact that a char in String can actually represent half the Unicode code point. (If the code point is not in BMP.) To deal with this, you need to iterate through the code points ... which is more complicated.
This approach will be faster than using String.split(/* clever regex*/) , and it will probably be faster than using Java 8+ threads. Most likely this is faster:
String[] res = new String[str.length()]; int 0 = 0; for (char ch: str.toCharArray[]) { res[i++] = Character.toString(ch); }
because toCharArray must copy the characters into a new array.
Stephen C Jan 27 '17 at 8:03 on 2017-01-27 08:03
source share