Comparing a char to a code-point?

前端 未结 5 1729
不知归路
不知归路 2020-12-01 02:15

What is the \"correct\" way of comparing a code-point to a Java character? For example:

int codepoint = String.codePointAt(0);
char token = \'\\n\';
<         


        
5条回答
  •  暖寄归人
    2020-12-01 02:32

    The Character class contains many useful methods for working with Unicode code points. Note methods like Character.toChars(int) that return an array of chars. If your codepoint lies in the supplementary range, then the array will be two chars in length.

    How you want to compare the values depends on whether you want to support the full range of Unicode values. This sample code can be used to iterate through a String's codepoints, testing to see if there is a match for the supplementary character MATHEMATICAL_FRAKTUR_CAPITAL_G (𝔊 - U+1D50A):

    public final class CodePointIterator {
    
      private final String sequence;
      private int index = 0;
    
      public CodePointIterator(String sequence) {
        this.sequence = sequence;
      }
    
      public boolean hasNext() {
        return index < sequence.length();
      }
    
      public int next() {
        int codePoint = sequence.codePointAt(index);
        index += Character.charCount(codePoint);
        return codePoint;
      }
    
      public static void main(String[] args) {
        String sample = "A" + "\uD835\uDD0A" + "B" + "C";
        int match = 0x1D50A;
        CodePointIterator pointIterator = new CodePointIterator(sample);
        while (pointIterator.hasNext()) {
          System.out.println(match == pointIterator.next());
        }
      }
    }
    

    For Java 8 onwards CharSequence.codePoints() can be used:

    public static void main(String[] args) {
      String sample = "A" + "\uD835\uDD0A" + "B" + "C";
      int match = 0x1D50A;
      sample.codePoints()
            .forEach(cp -> System.out.println(cp == match));
    }
    

    I created a table to help get a handle on Unicode string length and comparison cases that sometimes need to be handled.

提交回复
热议问题