Java - StringBuilder codePointAt() Method



The Java StringBuilder codePointAt() method returns the character (Unicode code point) at the specified index.

In Java, an index is nothing but, the position of character or element in the string or an array. The indices start from 0 and end with the length()-1, where the 0 index represents the first char value, and the length()-1 index represents the last char value in the given sequence.

The codePointAt() method accepts a parameter as an integer, where the integer represents the index value of the character.

Syntax

Following is the syntax of the java StringBuilder codePointAt() method −

public int codePointAt(int index)

Parameters

  • index − This is the index to the char values.

Return Value

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

Example

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

In the following program, we are creating a StringBuilder with the value "programming". We are trying to retrieve the character code point value at the specified index using the codePointAt() method.

package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      StringBuilder str = new StringBuilder("programming");
      System.out.println("string = " + str);
      
      // returns the codepoint at index 5
      int retval = str.codePointAt(5);
      System.out.println("Character(unicode point) = " + retval);
      str = new StringBuilder("amrood admin ");
      System.out.println("string = " + str);
      
      // returns the codepoint at index 6 i.e whitespace character
      retval = str.codePointAt(6);
      System.out.println("Character(unicode point) = " + retval);
   }
}  

Output

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

string = programming
Character(unicode point) = 97
string = amrood admin
Character(unicode point) = 32

Example

If the index value is greater than the length of the given sequence, this method will throw an IndexOutOfBoundException.

In this program, we are creating an object of the StringBuilder with the value “TutorialsPoint”. Using the codePointAt() method, we are trying to retrieve the code point value at the specified index whose value is greater than the string length.

package com.tutorialspoint.StringBuilder;
public class Test {
   public static void main(String[] args) {
      try {
         
         //create a StringBuilder
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         System.out.println("String: " + sb);
         
         //declare a integer variable that holds the index value
         int index = 20;
         System.out.println("The given code point value is: " + index);
         
         //using the charAt() method
         System.out.println("The char value of the " + index + " index is: " + sb.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

String: TutorialsPoint
The given code point value is: 20
java.lang.StringIndexOutOfBoundsException: index 20, length 14
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.AbstractStringBuilder.codePointAt(AbstractStringBuilder.java:382)
	at java.base/java.lang.StringBuilder.codePointAt(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Test.main(Test.java:12)
Exception: java.lang.StringIndexOutOfBoundsException: index 20, length 14

Example

We can retrieve the code point values of all characters using for loop.

In the following program, we are instantiating the StringBuilder class with the value “HelloWorld”. Using the codePointAt() method, we are trying to retrieve the code point (uni code) value of each character.

package com.tutorialspoint.StringBuilder;
public class Demo {
   public static void main(String[] args) {
      
      //create a StringBuilder
      StringBuilder sb = new StringBuilder("HelloWorld");
      System.out.println("String: " + sb);
      
      //declare a integer variable that holds the index value
      System.out.println("The code point of all the charceters: ");
      for(int i = 0; i<sb.length(); i++) {
         System.out.println("The code point value of " + sb.charAt(i) +" is: "+ sb.codePointAt(i));
      }
   }
}

Output

The above program, produces the following results −

String: HelloWorld
The code point of all the charceters: 
The code point value of H is: 72
The code point value of e is: 101
The code point value of l is: 108
The code point value of l is: 108
The code point value of o is: 111
The code point value of W is: 87
The code point value of o is: 111
The code point value of r is: 114
The code point value of l is: 108
The code point value of d is: 100

Example

If the index value is negative, this method throws an IndexOutOfBoundsException when we invoke this method on it.

In the following program, we are creating an object of the StringBuilder with the value "TutorialsPoint". We are trying to retrieve the code point value at -3 index using the codePointAt() method.

package com.tutorialspoint.StringBuilder;
public class Test {
   public static void main(String[] args) {
      try {
         //create a StringBuilder
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         System.out.println("String: " + sb);
         //declare a integer variable that holds the index value
         int index = -3;
         System.out.println("The given code point value is: " + index);
         //using the charAt() method
         System.out.println("The char value of the " + index + " index is: " + sb.codePointAt(index));
      } catch(IndexOutOfBoundsException e) {
          e.printStackTrace();
          System.out.println("Exception: " + e);
      }
   }
}

Output

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

String: TutorialsPoint
The given code point value is: -3
java.lang.StringIndexOutOfBoundsException: index -3, length 14
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.AbstractStringBuilder.codePointAt(AbstractStringBuilder.java:382)
Exception: java.lang.StringIndexOutOfBoundsException: index -3, length 14
	at java.base/java.lang.StringBuilder.codePointAt(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Test.main(Test.java:12)
java_lang_stringbuilder.htm
Advertisements