w3resource logo


Introduction to Java Programming

Java String: codePointAt() Method

Secondary Nav

public int codePointAt(int index)

The codePointAt() method is used to get the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.

If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.

Java Platform: Java SE 8

Syntax :

codePointAt(int index)

Parameters:

Name Description Type
index the index to the char values int

Return Value :
The code point value of the character at the index.

Return Value Type: int

Throws: IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

Example : Java String codePointAT() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args) {
System.out.println();
    String str = "w3resource.com";
System.out.println("Original String : " + str);

    // codepoint at index 1
int val1 = str.codePointAt(1);

    // codepoint at index 9
int val2 = str.codePointAt(9);

    // prints character at index1 in string
System.out.println("Character(unicode point) = " + val1);
    // prints character at index9 in string
System.out.println("Character(unicode point) = " + val2);
System.out.println();  
  }
}

Output:

Original String : w3resource.com                       
Character(unicode point) = 51                          
Character(unicode point) = 101 

Example of Throws: codePointAt(int index) Method

IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

Let

int val2 = str.codePointAt(-9);

in the above example.

Output:

Original String : w3resource.com                       
Exception in thread "main" java.lang.StringIndexOutOfBo
undsException: String index out of range: -9           
        at java.lang.String.codePointAt(String.java:687
)                                                      
        at Exercise.main(Example.java:12) 

Java Code Editor:







Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables