Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: String.toCharArray() with unicode characters

I know that char cannot contain Unicode characters (like char c = '\u1023'). So how would I go about doing

    String s = "ABCDEFG\u1023";
    char[] c = s.toCharArray();

I would like to convert s to a CharArray for performance reasons as I have to loop through every character in a potentially very long string which is inefficient. Anything which achieves the same result is fine.

Thanks a lot!

EDIT: Actually char can contain unicode chars. I'm just being stupid. Thanks to those who helped out anyway.

like image 633
Henry Thompson Avatar asked Oct 04 '11 18:10

Henry Thompson


People also ask

How do you write toCharArray in Java?

The java string toCharArray() method converts the given string into a sequence of characters. The returned array length is equal to the length of the string. Syntax : public char[] toCharArray() Return : It returns a newly allocated character array.

What is Unicode in Java string?

Unicode is a 16-bit character encoding system. The lowest value is \u0000 and the highest value is \uFFFF. UTF-8 is a variable width character encoding. UTF-8 has the ability to be as condensed as ASCII but can also contain any Unicode characters with some increase in the size of the file.


1 Answers

Whoever told you that in Java char can't contain Unicode characters, was wrong:

The values of the integral types are integers in the following ranges:

  • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
like image 158
NPE Avatar answered Oct 02 '22 14:10

NPE