Too many characters in character literal error

后端 未结 2 1675
忘掉有多难
忘掉有多难 2021-01-15 16:21

I\'m creating a stylish text app but on some places I\'m getting an error (\"Too many characters in character literal\"). I am writing only one letter but when I paste it c

相关标签:
2条回答
  • 2021-01-15 16:27

    A Java char stores a 16-bit value, i.e. can store 65536 different values. There are currently 137929 characters in Unicode (12.1).

    To handle this, Java strings are stored in UTF-16, which is a 16-bit encoding. Most Unicode characters, known as code points, are stored in a single 16-bit value. Some are stored in a pair of 16-bit values, known as surrogate pairs.

    This means that a Unicode character may be stored as 2 char "characters" in Java, which means that if you want your code to have full Unicode character support, you can't store a Unicode character in a single char value.

    They can be stored in an int variable, where the value is then referred to as a code point in Java. It is however often easier to store them as a String.

    In your case, you seem to be replacing Unicode characters, so a regex replacement call might be better, e.g.

    s = s.replaceAll("[Zz]", "\uD83C\uDD89");
    
    // Or like this if source file is UTF-8
    s = s.replaceAll("[Zz]", "                                                                    
    0 讨论(0)
  • 2021-01-15 16:42

    You can't do that with char data type. Use String instead.

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

    0 讨论(0)
提交回复
热议问题