Java - String codePointAt() Method



The Java String codePointAt() method is used to retrieve the character (Unicode code point) at the specified index. The index refers to the position of the char values of the given string and ranges from 0 to length()–1.

The first character value denotes the 0th index, the second character value denotes the 1st index value, and so on. The codePointAt() method accepts a parameter as an integer that holds the index value. It throws an exception if the index value is negative or the index value is greater than the string length.

Syntax

Following is the syntax of the Java String codePointAt() method −

public int codePointAt(int index)

Parameters

  • index − This is the index of the char value.

Return Value

This method returns the code point value of the character at the index.

Example

If the index value is positive and less than the string length, this method returns the character code point at the specified index.

In the following example, we are creating an object of the string class with the value “JavaProgramming”. Then, using the codePointAt() method, we are trying to retrieve the character code point value at index 3.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      
      //create an object of the string
      String str = new String("JavaProgramming");
      System.out.println("The given string is: " + str);
      
      // initialize the index value
      int index = 3;
      System.out.println("The given index value is: " + index);
      
      //using the codePointAt() method
      System.out.println("The character code point value of the " + index + "rd index is: " + str.codePointAt(index));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: JavaProgramming
The given index value is: 3
The character code point value of the 3rd index is: 97

Example

If the index value is greater than the string length, this method throws the StringIndexOutOfBoundsException.

In this example, we are creating a string literals with the value “HelloWorld”, and then we are using the codePointAt() method to print the character code point value at the specified index whose value is greater than the string length.

package com.tutorialspoint.String;
import java.lang.*;
public class Test {
   public static void main(String[] args) {
      try {
         
         // string initialization
         String str = "HelloWorld";
         System.out.println("The given string is: " + str);
         
         //initialize the index value
         int index = 15;
         System.out.println("The given index value is: " + index);
         
         //using the codePointAt() method
         System.out.print("The character code point value at the " + index + "th index is: " + str.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The given string is: HelloWorld
The given index value is: 15
java.lang.StringIndexOutOfBoundsException: index 15, length 10
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.String.codePointAt(String.java:1545)
	at com.tutorialspoint.String.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: index 15, length 10

Example

Using for loop along with the codePointAt() method, we can print the all character code point value of the given string.

In the following example, we are instantiating the string class with the value “HelloWorld”. Then, using the codePointAt() method along with the for loop to print the all-character code point value of the given string.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class TP {
   public static void main(String[] args) {
      
      // decalre a string
      String string;
      
      // initialize it
      string = "TutorialsPoint";
      System.out.print("The string value is: " + string);
      
      // declare integer variables
      System.out.print("\nThe uni code point of the characters are: ");
      for (int i = 0; i < string.length(); i++) {
         System.out.print(string.codePointAt(i) + ",");
      }
   }
}

Output

The above program, produces the following results −

The string value is: TutorialsPoint
The uni code point of the characters are: 84,117,116,111,114,105,97,108,115,80,111,105,110,116,

Example

If the given index value is a negative value, the codePointAt() method throws a StringIndexOutOfBoundsException.

package com.tutorialspoint.String;
import java.lang.*;
public class Test {
   public static void main(String[] args) {
      try {
       
         // create an object of the string
         String str = new String("TutorialsPoint");
         System.out.println("The given string is: " + str);
         
         //initialize the index value
         int index = -1;
         System.out.println("The given index value is: " + index);
         
         //using the codePointAt() method
         System.out.print("The character code point value at the " + index + "the index is: " + str.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
          e.printStackTrace();
          System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it will produce the following output −

The given string is: TutorialsPoint
The given index value is: -1
java.lang.StringIndexOutOfBoundsException: index -1, length 14
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.String.codePointAt(String.java:1545)
	at com.tutorialspoint.String.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: index -1, length 14
java_lang_string.htm
Advertisements