Roy_Zhang82 发表于 2013-2-3 14:18:38

复习Java核心技术——卷1,第3章,Code Point

趁着国庆节放假,好好复习一下Java核心技术这本好书。
/** * 实验表明,GBK比UTF-8所需要的codepoint更多,和UTF-16差不多。 * 一个代码点(code point)可能有一到两个代码单元(code unit) * @author Roy Zhang * */public class TestCodePoint{    public static void main(String[] args)    {      String greeting = "Hello★";      int n = greeting.length();      System.out.println(n); // is 5.      int cpCount = greeting.codePointCount(0, greeting.length());      System.out.println(cpCount); // is 5.      char first = greeting.charAt(0);      System.out.println(first); // is H      char last = greeting.charAt(greeting.length() - 1);      System.out.println(last); // UTF-16 and GBK will show ★      for (int i = 0; i < greeting.codePointCount(0, greeting.length()); i++)      {            int index = greeting.offsetByCodePoints(0, i);            int cp = greeting.codePointAt(index);            System.out.println(cp);// they are: 72, 101, 108, 108, 111, 9733      }      for (int i = 0; i < greeting.codePointCount(0, greeting.length()); )      {            int cp = greeting.codePointAt(i);            System.out.println(greeting.charAt(i));// they are: H, e, l, l, o, ★            if (Character.isSupplementaryCodePoint(cp))            {                // It does not work.                i = i + 2;            }            else            {                i++;            }      }    }}
页: [1]
查看完整版本: 复习Java核心技术——卷1,第3章,Code Point