Java - StringBuilder codePointBefore() Method



The Java StringBuilder codePointBefore() method returns the character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length().

In Java, the indexes are nothing but the position of the characters in the sequence. A code point is a numerical value that maps to a specific character. The codePointBefore() method accepts a parameter as an integer that holds the value of the index. It throws an exception if the index value is less than 1 or greater than the given string length.

Syntax

Following is the syntax of the Java StringBuilder codePointBefore() method −

public int codePointBefore(int index)

Parameters

  • index − This is the index following the code point that should be returned.

Return Value

This method returns the Unicode code point value before the given index.

Example

If the index value is greater than 0 and less than the sequence length, it returns the character code point before the specified index.

In this program, we are creating an object of the StringBuilder with the value “programming”. Using the codePointBefore() method, we are trying to retrieve the character code point at the 3rd index and 6th index.

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      
      //create an object of the StringBuilder
      StringBuilder sb = new StringBuilder("programming");
      System.out.println("String is: " + sb);
      
      //initialize the index values
      int index1 = 3;
      int index2 = 6;
      System.out.println("The index values are: " + index1 + " and " + index2);
      
      //using the codePointBefore() method
      int codepoint1 = sb.codePointBefore(index1);
      int codepoint2 = sb.codePointBefore(index2);
      System.out.println("The character code point before the " + index1 + "rd index is: " + codepoint1);
      System.out.println("The character code point before the " + index2 + "th index is: " + codepoint2);
   }
}

Output

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

String is: programming
The index values are: 3 and 6
The character code point before the 3rd index is: 111
The character code point before the 6th index is: 97

Example

If the index value is less than 1, this method throws an IndexOutOfBoundsException.

In the following example,we are instantiating the StringBuilder class with the value “HelloWorld”. Using the codePointBefore() method, we are trying to retrieve the character code point at the 0 index.

package com.tutorialspoint.StringBuilder;
public class Test {
   public static void main(String[] args) {
      try {
         
         //create an object of the StringBuilder
         StringBuilder sb = new StringBuilder("HelloWorld");
         System.out.println("String is: " + sb);
         
         //initialize the index value
         int index = 0;
         System.out.println("The index value is: " + index);
         
         //using the codePointBefore() method
         int codepoint = sb.codePointBefore(index);
         System.out.println("The character code point before the " + index + "rd index is: " + codepoint);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

String is: HelloWorld
The index value is: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.base/java.lang.AbstractStringBuilder.codePointBefore(AbstractStringBuilder.java:413)
	at java.base/java.lang.StringBuilder.codePointBefore(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Test.main(Test.java:12)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

Example

If the index value is greater than the sequence length, this method throws an IndexOutOfBoundsException.

In this example, we create a StringBuilder with the value“TutorialsPoint”. Using the codePointBefore() method, we are trying to retrieve the character code point at the index whose value is greater than the sequence length

package com.tutorialspoint.StringBuilder;
public class Demo {
   public static void main(String[] args) {
      try {
          
          //create an object of the StringBuilder
          StringBuilder sb = new StringBuilder("TutorialsPoint");
          System.out.println("String is: " + sb);
          
          //initialize the index value
          int index = 20;
          System.out.println("The index value is: " + index);
          
          //using the codePointBefore() method
          int codepoint = sb.codePointBefore(index);
          System.out.println("The character code point before the " + index + "rd index is: " + codepoint);
      } catch(IndexOutOfBoundsException e) {
          e.printStackTrace();
          System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

String is: TutorialsPoint
The index value is: 20
java.lang.StringIndexOutOfBoundsException: String index out of range: 20
	at java.base/java.lang.AbstractStringBuilder.codePointBefore(AbstractStringBuilder.java:413)
	at java.base/java.lang.StringBuilder.codePointBefore(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Demo.main(Demo.java:12)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 20
java_lang_stringbuilder.htm
Advertisements