String codePointAt(int index) method Java

Let’s learn String codePointAt(int index) method java.

String codePointAt(int index) method Java

String codePointAt(int index) method the code point value of the character at the index. The index refers to char values(Unicode code units) and ranges from 0 to length() – 1.

Syntax:

public int codePointAt(int index)

Parameters:

index the index to the char values.

Throws:

IndexOutOfBoundsException – if the indexargument is negative or not less than the length of this string.

Example:

class StringCodePointAtMethodDemo 
{
    public static void main(String[] args) 
    {
        String str = new String("HelloWorld");
        System.out.println("Given string : " + str);  
        // index value
        int index = 2;
        System.out.println("Index value is: " + index);        
        System.out.println("Character Unicode point value of the " + index + "nd index is : " + str.codePointAt(index));
    }
}

Output:

Given string : HelloWorld
Index value is: 2
Character Unicode point value of the 2nd index is : 108


Let’s see another example on String codePointAt(int index) method where index value is greater than length of given string. Here codePointAt(int index) throws StringIndexOutOfBoundsException.

class StringCodePointAtMethodDemo1 
{
    public static void main(String[] args) 
    {
        try 
        {
            String s = "JavaProgram";
            System.out.println("Given string : " + s);
            int index = 24;
            System.out.println("Index value : " + index);
            System.out.print("Character unicode point value at the " + index + "th index is: " + s.codePointAt(index));
        }
        catch(IndexOutOfBoundsException ex) 
        {
            ex.printStackTrace();
            System.out.println("Exception : " + ex);
        }
    }
}

Output:

Given string : JavaProgram
Index value : 24
java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 11
at java.base/java.lang.String.checkIndex(String.java:4570)
at java.base/java.lang.String.codePointAt(String.java:1557)
at ArraysInJava/com.Strings.java.StringCodePointAtMethodDemo1.main(StringCodePointAtMethodDemo1.java:13)
Exception : java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 11


Similar to the above program if the index value has negative value then String codePointAt(int index) method throws StringIndexOutOfBoundsException.

class StringCodePointAtMethodDemo1 
{
    public static void main(String[] args) 
    {
        try 
        {
            String s = "JavaProgram";
            System.out.println("Given string : " + s);
            int index = -1;
            System.out.println("Index value : " + index);
            System.out.print("Character unicode point value at the " + index + "th index is: " + s.codePointAt(index));
        }
        catch(IndexOutOfBoundsException ex) 
        {
            ex.printStackTrace();
            System.out.println("Exception : " + ex);
        }
    }
}

Output:

Given string : JavaProgram
Index value : -1
java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 11
at java.base/java.lang.String.checkIndex(String.java:4570)
at java.base/java.lang.String.codePointAt(String.java:1557)
at ArraysInJava/com.Strings.java.StringCodePointAtMethodDemo1.main(StringCodePointAtMethodDemo1.java:13)
Exception : java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 11


Also read – Encapsulation in java