Java StringBuilder.codePointCount() – Examples

In this tutorial, we will learn about the Java StringBuilder.codePointCount() function, and learn how to use this function to count number of Unicode code points in the specified range, with the help of examples.

codePointCount(int beginIndex, int endIndex)

StringBuilder.codePointCount() returns the number of Unicode code points in the specified text range of this StringBuilder sequence. The range is specified by the parameters: beginIndex and endIndex.

ADVERTISEMENT

Syntax

The syntax of codePointCount() function is

codePointCount(int beginIndex, int endIndex)

where

ParameterDescription
beginIndexAn integer, specifying the index to the first char of the text range.
endIndexAn integer, specifying the index to the last char of the text range.

Returns

The function returns an integer value.

Example 1 – codePointCount()

In this example, we will take a StringBuilder and find the number of Unicode code points in the index range (2, 5).

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh");

        int beginIndex = 2;
        int endIndex = 5;
        int count= stringBuilder.codePointCount(beginIndex, endIndex); 
        System.out.println("Code Point count in the specified range is: " + count);   
    }
}

Output

Code Point count in the specified range is: 3

Example 2 – codePointCount()

In this example, we will give end value greater than the length of the sequence in StringBuilder. codePointCount() throws java.lang.IndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh");

        int beginIndex = 1;
        int endIndex = 15;
        int count= stringBuilder.codePointCount(beginIndex, endIndex); 
        System.out.println("Code Point count in the specified range is: " + count);   
    }
}

Output

Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.base/java.lang.AbstractStringBuilder.codePointCount(Unknown Source)
	at java.base/java.lang.StringBuilder.codePointCount(Unknown Source)
	at Example.main(Example.java:8)

Example 3 – codePointCount()

In this example, we will give start value greater than end value. codePointCount() throws java.lang.IndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        // create a StringBuilder object 
        StringBuilder stringBuilder = new StringBuilder("abcdefgh");

        int beginIndex = 4;
        int endIndex = 1;
        int count= stringBuilder.codePointCount(beginIndex, endIndex); 
        System.out.println("Code Point count in the specified range is: " + count);   
    }
}

Output

Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.base/java.lang.AbstractStringBuilder.codePointCount(Unknown Source)
	at java.base/java.lang.StringBuilder.codePointCount(Unknown Source)
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.codePointCount() function, and also learnt how to use this function with the help of examples.