Java String: codePointBefore() Method
public int codePointBefore(int index)
The codePointBefore() method is used to get a character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length.
If the char 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 char value at index - 1 is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.
Java Platform: Java SE 8
Syntax:
codePointBefore(int index)
Parameters:
Name | Description | Type |
---|---|---|
index | the index following the code point that should be returned | int |
Return Value:
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework