STRING INSTANCE METHODS
Java String codePointBefore() method
codePointBefore() returns the Unicode code point before the specified String index. The supplied index marks the position after the desired character, not the character itself.
Your first code point before an index
String tekstas = "Hello";
int kodas = tekstas.codePointBefore(1);
System.out.println(kodas);72
- The variable
tekstasstores the value Hello. codePointBefore(1)reads the code point before index1.- Before index
1is the first character,H, at index0. - The Unicode code point for the letter
His72.
Why is codePointBefore() an instance method?
codePointBefore() is called on a specific string because it reads a code point from that string:
int kodas = tekstas.codePointBefore(1);
We therefore do not write String.codePointBefore(...). The method needs a specific String object and an index before which to read the Unicode code point.
Method syntax
public int codePointBefore(int index)
| Part | Meaning |
|---|---|
| Calling object | tekstas in the line tekstas.codePointBefore(1) |
| Parameter | index – the position after the code point to retrieve |
| Return type | int |
| Valid indices | From 1 to length() |
| Error | IndexOutOfBoundsExceptionwhen the index is 0, negative or greater than length() |
The index marks the position after a character
String tekstas = "Java";
System.out.println(tekstas.codePointBefore(1));
System.out.println(tekstas.codePointBefore(2));
System.out.println(tekstas.codePointBefore(3));
System.out.println(tekstas.codePointBefore(4));74
97
118
97
The text Java has the indices 0, 1, 2 and 3. However, codePointBefore(1) returns the value from index 0, codePointBefore(2) returns the value from index 1, and so on.
The difference from codePointAt()
String tekstas = "Java";
int nuoIndekso = tekstas.codePointAt(1);
int priesIndeksa = tekstas.codePointBefore(1);
System.out.println(nuoIndekso);
System.out.println(priesIndeksa);97
74
codePointAt(1) reads from index 1, obtaining the letter's a code point, 97. codePointBefore(1) reads before index 1, obtaining the letter's J code point, 74.
Emoji and supplementary Unicode characters
String tekstas = "A😀B";
System.out.println(tekstas.length());
System.out.println(tekstas.codePointBefore(3));4
128512
The text A😀B looks like three characters to a person, but Java's String.length() returns 4because the emoji uses two UTF-16 code units. When codePointBefore(3) looks before index 3, it recognizes the emoji's surrogate pair and returns its complete code point, 128512.
First and last code point
String tekstas = "Kodas";
int pirmas = tekstas.codePointBefore(1);
int paskutinis = tekstas.codePointBefore(tekstas.length());
System.out.println(pirmas);
System.out.println(paskutinis);75
115
- Retrieve the first code point using
codePointBefore(1)because it is before position1. - Retrieve the last code point using
codePointBefore(tekstas.length()). - The letter's
KUnicode code point is75. - The letter's
sUnicode code point is115.
Traversing code points backwards safely
String tekstas = "A😀B";
for (int i = tekstas.length(); i > 0;) {
int kodas = tekstas.codePointBefore(i);
System.out.println(kodas);
i -= Character.charCount(kodas);
}66
128512
65
codePointBefore() is useful for reading text from end to start. Character.charCount(kodas) tells you whether the code point uses one or two UTF-16 units, preventing the loop from stopping inside a surrogate pair.
Index 0 is invalid
String tekstas = "Java";
System.out.println(tekstas.codePointBefore(0));StringIndexOutOfBoundsException
Before index 0 there is no character in the string, so codePointBefore(0) is always an error. This is the key difference from codePointAt(0), which reads the first code point.
The index cannot exceed length()
String tekstas = "Java";
System.out.println(tekstas.codePointBefore(5));StringIndexOutOfBoundsException
Java has length 4, so the largest valid argument is 4. codePointBefore(5) tries to read outside the string and throws StringIndexOutOfBoundsException, a kind of IndexOutOfBoundsException exception.
Handling an error
String tekstas = "Java";
int indeksas = 0;
try {
System.out.println(tekstas.codePointBefore(indeksas));
} catch (IndexOutOfBoundsException e) {
System.out.println("Netinkamas indeksas");
}Netinkamas indeksas
In practice, it is better to check bounds before calling: indeksas > 0 && indeksas <= tekstas.length(). try-catch helps when the index comes from user input or another external source.
Common mistakes
- Assuming that
codePointBefore(0)retrieves the first character, although before index0there is nothing. - Confusing it with
codePointAt():codePointAt(index)reads from the index, whilecodePointBefore(index)reads before the index. - Using this for the last code point:
codePointBefore(length() - 1), when the correct value iscodePointBefore(length()). - Moving backwards by
i--, even though the text may contain emoji or other characters requiring two UTF-16 units. - Calling the method on an empty string or with an index greater than
length().
When should you use codePointBefore()?
codePointBefore() Use it to read a Unicode code point before the current position: traversing text backwards, checking the character before a cursor, processing emoji or writing logic where a simple char may be insufficient. To read forward from a particular index, use codePointAt().