Scala获取字符串的代码点

时间:2014-03-15 03:49:45

标签: java string scala unicode

我正努力做好,并与Unicode兼容。

不幸的是,Java并没有让这很容易。

creating and using Strings with surrogate pairs

public static Iterable<Integer> getCodePoints(final String text) {
    return new Iterable<Integer>() {
        @Override public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private int nextIndex = 0;

                @Override public boolean hasNext() {
                    return nextIndex < text.length();
                }

                @Override public Integer next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }
                    int codePoint = text.codePointAt(nextIndex);
                    nextIndex += Character.charCount(codePoint);
                    return codePoint;
                }

                @Override public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}

Scala能否更轻松地获取String的代码点?

0 个答案:

没有答案