简体   繁体   中英

Extracting Double Byte Characters/substring from a UTF-8 formatted String

I'm trying to extract emojis and other special Characters from Strings for further processing (eg a String contains '😅' as one of its Characters).

But neither string.charAt(i) nor string.substring(i, i+1) work for me. The original String is formatted in UTF-8 and this means, that the escaped form of the above emoji is encoded as '\?\?'. That's why I receive '?' (\?) and '?' (\?) instead for this position, causing it to be at two positions when iterating over the String.

Does anyone have a solution to this problem?

Thanks to John Kugelman for the help. the solution looks like this now:

for(int codePoint : codePoints(string)) {

        char[] chars = Character.toChars(codePoint);
        System.out.println(codePoint + " : " + String.copyValueOf(chars));

    }

With the codePoints(String string)-method looking like this:

private static Iterable<Integer> codePoints(final String string) {
    return new Iterable<Integer>() {
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                int nextIndex = 0;

                public boolean hasNext() {
                    return nextIndex < string.length();
                }

                public Integer next() {
                    int result = string.codePointAt(nextIndex);
                    nextIndex += Character.charCount(result);
                    return result;
                }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM