public class Qqqq {
    
    public static void main(String[] args) {
     String s="Hello";
     int n=s.length();
     int cpCont=s.codePointCount(0,s.length());
     int index=s.offsetByCodePoints(0,2);
     int cp=s.codePointAt(index);
       
       System.out.println(n);
       System.out.println(cpCont);
       System.out.println(index);
       System.out.println(cp);
    }
}
运行结果是5 5 2 108
请问什么是代码点,和代码单元,有什么用处啊?不懂

解决方案 »

  1.   

    Unicode 代码点(即字符)
    Unicode 代码单元(即 char 值)
      

  2.   

    1.  int n=s.length();
       返回字符串的长度,这个不用解释了。
    2.  int cpCont=s.codePointCount(0,s.length());
       返回此 String 的指定文本范围中的 Unicode 代码点数,即指定范围的字符数,你的程序中是0~字符串最后,所以返回等于字符串长度。5
    3.  int index=s.offsetByCodePoints(0,2);
       返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点(字符)的索引(索引值得就是位置)。你的程序中从第0个向右偏移2个字符,那么索引就是第2个了。
    4。 int cp=s.codePointAt(index);
       返回指定索引处的字符(Unicode 代码点)。你的程序中返回索引2处的字符,即第一个l的unicode值就是108.