w3resource

Java String: codePointBefore() Method

codePointBefore() method - Gets the character (Unicode code point) before the specified index

The codePointBefore() method is used to get a character (Unicode code point) before the specified index. The index refers to character values (Unicode code units) and ranges from 1 to length.

If the character value at (index - 1) is in the low-surrogate range, (index - 2) is not negative, and the char value at (index - 2) is in the high-surrogate range, then the supplementary code point value of the surrogate pair is returned. If the character value at index - 1 is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.

Java Platform: Java SE 8 and above

Syntax codePointBefore() method

codePointBefore(int index)

Parameters codePointBefore() method

Name Description Type
index The index following the code point that should be returned. int

Return Value codePointBefore() method

  • The Unicode code point value before the given index.

Return Value Type: int

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

Example: Java String codePointBefore() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args) {
System.out.println();
    String str = "w3resource.com";
System.out.println("Original String : " + str);

    // codepoint before index 1
int val1 = str.codePointBefore(1);

   // codepoint before index 9
int val2 = str.codePointBefore(9);

    // prints character before index1 in string
System.out.println("Character(unicode point) = " + val1);
    // prints character before index9 in string
System.out.println("Character(unicode point) = " + val2);
System.out.println();
  }
}

Output:

Original String : w3resource.com                       
Character(unicode point) = 119                         
Character(unicode point) = 99 

Example of Throws: codePointBefore(int index) Method

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

Let

int val2 = str.codePointBefore(-9);

in the above example.

Output:

Original String : w3resource.com                       
Exception in thread "main" java.lang.StringIndexOutOfBo
undsException: String index out of range: -9           
        at java.lang.String.codePointBefore(String.java
:717)                                                  
        at Exercise.main(Example.java:12)

Java Code Editor:

Previous:codePointAt Method
Next:codePointCount Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/string/string_codepointbefore.php