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);
Console output

72

  1. The variable tekstas stores the value Hello.
  2. codePointBefore(1) reads the code point before index 1.
  3. Before index 1 is the first character, H, at index 0.
  4. The Unicode code point for the letter H is 72.

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)

Parts of the codePointBefore() method
PartMeaning
Calling objecttekstas in the line tekstas.codePointBefore(1)
Parameterindex – the position after the code point to retrieve
Return typeint
Valid indicesFrom 1 to length()
ErrorIndexOutOfBoundsExceptionwhen 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));
Console output

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);
Console output

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));
Console output

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);
Console output

75
115

  1. Retrieve the first code point using codePointBefore(1)because it is before position 1.
  2. Retrieve the last code point using codePointBefore(tekstas.length()).
  3. The letter's K Unicode code point is 75.
  4. The letter's s Unicode code point is 115.

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);
}
Console output

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));
Result

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));
Result

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");
}
Console output

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 index 0 there is nothing.
  • Confusing it with codePointAt(): codePointAt(index) reads from the index, while codePointBefore(index) reads before the index.
  • Using this for the last code point: codePointBefore(length() - 1), when the correct value is codePointBefore(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().