Java String codePointBefore() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, codePointBefore() returns the Unicode code point that ends just before a given index. Use it when you walk a string from right to left.

You pass one index. It is required and must be greater than 0 and at most length(). The result is an int.

Syntax

Call codePointBefore on the string. Start with this line.

java
int cp = text.codePointBefore(text.length());
CallWhat it does
text.codePointBefore(text.length())Returns the last Unicode code point in the string.

Index 0 is never valid. Step back with Character.charCount when you walk left through surrogate pairs.

Library declarations
java
public int codePointBefore(int index);

Parameters

ParameterDescription
indexRequired. The index after the code point you want. Must be from 1 to length().

Return Value

Return ValueDescription
intThe Unicode code point before that index.

Java Examples

Example 1 — Character Left of a Cursor

For BMP text, codePointBefore(1) returns the first letterβ€” the unit sitting immediately left of index 1.

java
public class CodePointBeforeBasic {
    public static void main(String[] args) {
        String word = "Java";

        int cp = word.codePointBefore(1);

        System.out.println("codePointBefore(1) = " + cp);
        System.out.println("as char = " + (char) cp);
    }
}

How It Works

  1. Start with text. "Java" has length 4; units sit at indexes 0–3.
  2. Place the cursor at 1. That gap is between 'J' and 'a'.
  3. Look left. codePointBefore(1) returns the code point for 'J' (74).
  4. Cast for display. For BMP letters, (char) cp prints the glyph.

Example 2 — Last Character Including Emoji

codePointBefore(length()) returns the last code pointβ€” even when it is a two-unit emoji. Compare with codePointAt at the emoji’s start index.

java
public class CodePointBeforeEmoji {
    public static void main(String[] args) {
        String text = "HiπŸ˜€";

        int last = text.codePointBefore(text.length());
        int at   = text.codePointAt(2);

        System.out.println("length() = " + text.length());
        System.out.println("before(length) = 0x" + Integer.toHexString(last).toUpperCase());
        System.out.println("at(2)          = 0x" + Integer.toHexString(at).toUpperCase());
    }
}

How It Works

  1. Map the string. 'H', 'i', then a surrogate pair for πŸ˜€ (indexes 2–3).
  2. Cursor at the end. length() is 4β€”the gap after the low surrogate.
  3. Look left. codePointBefore(4) combines both surrogates β†’ U+1F600.
  4. Same emoji forward. codePointAt(2) starts at the high surrogate and returns the same value.

Example 3 — Walk the String Backward

Start the cursor at length(), read with codePointBefore, then step back by Character.charCount(cp). For larger jumps, see offsetByCodePoints().

java
public class CodePointBeforeReverse {
    public static void main(String[] args) {
        String text = "AπŸ˜€B";

        for (int i = text.length(); i > 0; ) {
            int cp = text.codePointBefore(i);
            System.out.println("before " + i + " β†’ 0x" + Integer.toHexString(cp).toUpperCase());
            i -= Character.charCount(cp);
        }
    }
}

How It Works

  1. Start at the end. Cursor i = length() (4 for "AπŸ˜€B").
  2. Read left. codePointBefore(4) β†’ 'B'; then subtract 1 β†’ cursor 3.
  3. Handle the emoji. codePointBefore(3) β†’ full πŸ˜€; charCount is 2 β†’ cursor 1.
  4. Finish at the start. codePointBefore(1) β†’ 'A'; subtract 1 β†’ loop ends.

Frequently Asked Questions

codePointBefore() returns the Unicode code point that ends just before a given index.
index is required. Valid values are from 1 to length().
An int Unicode code point.
No. Index 0 throws StringIndexOutOfBoundsException.
Use it when scanning a string from right to left by code points.

Next: codePointCount()

Learn how to count Unicode code points in a string range.

codePointCount() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful