w3resource

Java String: codePointAt() Method

codePointAt() method - Gets the character (Unicode code point) at the specified 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.

Java Platform: Java SE 8 and above.

Syntax codePointAt() method

codePointAt(int index)

Parameters codePointAt() method

Name Description Type
index The index to the character values int

Return Value codePointAt() method

  • 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:

Previous:charAt Method
Next:codePointBefore Method



Follow us on Facebook and Twitter for latest update.