Java - String codePointBefore() Method



The Java String codePointBefore() method returns the character code point value (Unicode code point) before the specified index. The index refers to character values' position in the given string, (Unicode code units) and ranges from 1 to length.

The index value starts from 1 and ends with string length, because, if we start the index value from 0 then it will start searching for the character value before the zero index value and that is -1. In this case, the codePointBefore() method will throw an exception.

The codePointBefore() method accepts an integer parameter that holds the index value, it throws an exception when the index value is less than 1 or greater than the string length.

Syntax

Following is the syntax of the Java String 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 given index value is zero, the codePointBefore() method throws a StringIndexOutOfBoundsException.

In the following example, we are instantiating the string class with the value “HelloWorld”.Then, using the codePointBefore() method, we are trying to retrieve the character code point value before the specified index 0.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         //creating an object of the string class
         String str = new String("HelloWorld");
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = 0;
         System.out.println("The given index value is: " + index);
         
         //using the codePointBefore() method
         System.out.println("The character code point value before specoied index " + index + " is: " + str.codePointBefore(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

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

The string value is: HelloWorld
The given index value is: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.base/java.lang.String.codePointBefore(String.java:1578)
	at com.tutorialspoint.String.Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

Example

If the given index value is negative, this method throws StringIndexOutOfBoundsException.

In this example, we are creating a string literal with the value “TutorialsPoint”. Then, using the codePointBefore() method, we are trying to retrieve the character code point value before the specified index -1.

import java.lang.*;
public class Test {
   public static void main(String[] args) {
      try {
         
         // Initialized the string literal
         String str = "TutorialsPoint";
         System.out.println("The string value is: " + str);
         
         //initialize the index value
         int index = -1;
         System.out.println("The given index values is: " + index);
         
         //using the codePointBefore() method
         System.out.println("The character code point value before the specifed index " + index + " is: " + str.codePointBefore(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The string value is: TutorialsPoint
The given index values is: -1
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.base/java.lang.String.codePointBefore(String.java:1578)
	at com.tutorialspoint.String.Test.main(Test.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

Example

If the given index value is positive and less than the string length, the codePointBefore() method returns the character code point before the specified index.

In the following program, we are creating an object of the String class with the value “Tutorix”. Then, using the codePointBefore() method, we are trying to retrieve the character code point value before the specified indexes 2 and 4.

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("Tutorix");
      System.out.println("The string value is: " + str);
      
      //initialize the index values
      int index1 = 2;
      int index2 = 4;
      System.out.println("The given index values are: " + index1 + " and " + index2);
      
      //using the codePointBefore() method
      System.out.println("The character code point value before the specified index " + index1 + " is: " + str.codePointBefore(index1));
      System.out.println("The character code point value before the specified index " + index2 + " is: " + str.codePointBefore(index2));
   }
}

Output

The above program, produces the following results −

The string value is: Tutorix
The given index values are: 2 and 4
The character code point value before the specified index 2 is: 117
The character code point value before the specified index 4 is: 111
java_lang_string.htm
Advertisements