String codePointBefore(int index) method Java

Let’s learn String codePointBefore(int index) method java.

String codePointBefore(int index) method Java

String codePointBefore(int index) 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.

Syntax:

public int codePointBefore(int index)

Parameters:

index the index following the code point that should be returned.

Throws

IndexOutOfBoundsException – if the index argument is less than 1 or greater than the length of this string.

Example

If the index value in String codePointBefore(int index) method is positive and less than the length of the given string then codePointBefore() method returns the character code point before the specified index.

class StringCodePointBeforeMethodDemo 
{
    public static void main(String[] args) 
    {
        String str = new String("HelloWorld");
        System.out.println("Given string : " + str);
        // initializing index value
        int index1 = 1;
        int index2 = 6;
        System.out.println("Character unicode point value before specified index " + index1 + " is : " + str.codePointBefore(index1));
        System.out.println("Character unicode point value before specified index " + index2 + " is : " + str.codePointBefore(index2));
   }
}

Output:

Given string : HelloWorld
Character unicode point value before specified index 1 is : 72
Character unicode point value before specified index 6 is : 87


If the index value in String codePointBefore(int index) method is zero then codePointBefore() method throws StringIndexOutOfBoundsException.

class StringCodePointBeforeMethodDemo 
{
   public static void main(String[] args) 
   {
      try 
      {
         String str = new String("HelloWorld");
         System.out.println("Given string : " + str);
         int index = 0;
         System.out.println("The given index value is: " + index);
         System.out.println("Character unicode point value before specified index " + index + " is: " + str.codePointBefore(index));
      }
      catch(IndexOutOfBoundsException ex) 
      {
         ex.printStackTrace();
         System.out.println("Exception: " + ex);
      }
   }
}

Output:

Given string : HelloWorld
The given index value is: 0
java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 10
at java.base/java.lang.String.codePointBefore(String.java:1589)
at ArraysInJava/com.Strings.java.StringCodePointBeforeMethodDemo.main(StringCodePointBeforeMethodDemo.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 10


If the index value in String codePointBefore(int index) method is negative then codePointBefore() method throws StringIndexOutOfBoundsException.

class StringCodePointBeforeMethodDemo 
{
   public static void main(String[] args) 
   {
      try 
      {
         String str = "HelloWorld";
         System.out.println("Given string : " + str);
         int index = -1;
         System.out.println("The given index value is: " + index);
         System.out.println("Character unicode point value before specified index " + index + " is: " + str.codePointBefore(index));
      }
      catch(IndexOutOfBoundsException ex) 
      {
         ex.printStackTrace();
         System.out.println("Exception: " + ex);
      }
   }
}

Output:

Given string : HelloWorld
The given index value is: -1
java.lang.StringIndexOutOfBoundsException: Index -2 out of bounds for length 10
at java.base/java.lang.String.codePointBefore(String.java:1589)
at ArraysInJava/com.Strings.java.StringCodePointBeforeMethodDemo.main(StringCodePointBeforeMethodDemo.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: Index -2 out of bounds for length 10


Also read – for loop in java