CodePointCount and OffsetByCodePoints

Java-CodePointCount and OffsetByCodePoints Methods


CodePointCount and OffsetByCodePoints Methods

Method Description
static int codePointCount​(CharSequence seq, int beginIndex, int endIndex) Returns the number of Unicode code points in the text range of the specified char sequence. The text range begins at the specified beginIndex and extends to the char at index endIndex – 1. Thus the length (in chars) of the text range is endIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.
static int codePointCount​(char[] a, int offset, int count) Returns the number of Unicode code points in a subarray of the char array argument. The offset argument is the index of the first char of the subarray and the count argument specifies the length of the subarray in chars. Unpaired surrogates within the subarray count as one code point each.
static int offsetByCodePoints​(CharSequence seq, int index, int codePointOffset) Returns the index within the given char sequence that is offset from the given index by codePointOffset code points. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each.
static int offsetByCodePoints​(char[] a, int start, int count, int index, int codePointOffset) Returns the index within the given char subarray that is offset from the given index by codePointOffset code points. The start and count arguments specify a subarray of the char array. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each.


A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs. Index values refer to char code units, so a supplementary character uses two positions in a String.

CodePointCount OffsetByCodePoints

Program

CodePointCount-and-OffsetByCodePoints output

Program Source

public class Javaapp {
    
    public static void main(String[] args)  {
        
        String str = "A𨉂B𨊉𨋜CD🚂👥🍒E";
        int totelpoints = Character.codePointCount(str, 0, str.length());
        int getintex;
        int pointvalue;

        for(int point=0;point<totelpoints;point++)
        {
            getintex = Character.offsetByCodePoints(str, 0, point);
            pointvalue=Character.codePointAt(str, getintex);
            System.out.print("str["+getintex+"]->");
            System.out.println(Character.toChars(pointvalue));
        }
    }
}

Leave a Comment