Java Reference Library →

String codePointBefore() method - Syntax and Examples

String codePointBefore() method - Syntax and Examples

Learn about the Java String codePointBefore() method. This tutorial provides a detailed explanation, syntax, parameters, return value, and practical examples to help you understand how to use this method effect

This reference explains String codePointBefore() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

UTF-16 boundary: indexes count UTF-16 code units. For the two-unit emoji shown below, index 2 reads the complete preceding code point; index 1 sees only the leading surrogate and returns its code-unit value. A byte-based explanation is not accurate for Java String indexing.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

String codePointBefore() method - Syntax and Examples

The codePointBefore() method in Java's String class allows you to retrieve the code point (a Unicode character) that immediately precedes a specified index within the string. This is particularly useful when dealing with strings containing characters represented by more than one byte, like emojis or other special symbols.

Syntax

public int codePointBefore(int index)

Let's break down what this means. This method takes an integer as input and returns an integer representing a Unicode code point.

Parameters

ParameterDescription
indexThe index before which the code point is retrieved. Must be greater than 0 and less than or equal to the string length.

Return Value

The method returns an int representing the Unicode code point located immediately before the specified index.

If the index is 0, it means you're asking for the character before the very beginning of the string. In this case, the method throws an IndexOutOfBoundsException because there is no preceding character.

Examples

Example 1: Basic Usage - Ascii Characters

This example demonstrates how to use codePointBefore() with a simple string containing only ASCII characters. We'll extract the code point before index 2, which should be 'a'.

String str = "abc";
int index = 2;
int codepoint = str.codePointBefore(index);
System.out.println("Code point before index " + index + ": " + Integer.toHexString(codepoint));
Code point before index 2: 97

Explanation: The character at index 2 is 'c'. The method returns the code point of 'b' which is 97 (represented as hexadecimal `0x61`).

Example 2: Handling Emojis

This example demonstrates how to use codePointBefore() with a string containing an emoji. Emojis are often represented by multiple code points.

String str = "Hello😊World";
int index = 6;
int codepoint = str.codePointBefore(index);
System.out.println("Code point before index " + index + ": " + Integer.toHexString(codepoint));
Code point before index 6: 128522

Explanation: The emoji '😊' is represented by the Unicode code point U+1F60A, which has a decimal value of 128522. The method retrieves this code point because it precedes the specified index.

Example 3: IndexOutOfBoundsException

This example shows what happens when you attempt to call codePointBefore() with an invalid index (0). An IndexOutOfBoundsException is thrown.

String str = "abc";
int index = 0;
//The following line will throw IndexOutOfBoundsException.
//int codepoint = str.codePointBefore(index);
System.out.println("This won't print if the exception is thrown");
Exception in thread "main": java.lang.IndexOutOfBoundsException: index -1,size = 3

Explanation: Since `index` is 0, there is no character before it. The program throws an exception to indicate that the requested operation is not possible.

Example 4: String with surrogate pairs

This example demonstrates how codePointBefore() works when a string contains characters encoded using surrogate pairs (which represent Unicode code points outside the Basic Multilingual Plane).

String str = "\uD83D\uDE00"; // Represents 😀 Grinning Face
int index = 1;
int codepoint = str.codePointBefore(index);
System.out.println("Code point before index " + index + ": " + Integer.toHexString(codepoint));
Code point before index 1: 128512

Explanation: The string \uD83D\uDE00 represents the grinning face emoji. The codePointBefore() method correctly identifies and returns the preceding character's Unicode code point (128512).

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →