Too many characters in character literal error

后端 未结 2 1674
忘掉有多难
忘掉有多难 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]", "

提交回复
热议问题