Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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 the length of the string.

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).



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M