Java String codePointBefore() Method

The String.codePointBefore() method in Java is used to return the Unicode code point of the character before a specified index in a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. codePointBefore Method Syntax
  3. Examples
    • Getting the Code Point Before a Character
    • Handling Supplementary Characters
    • Handling Invalid Index
  4. Conclusion

Introduction

The String.codePointBefore() method is a member of the String class in Java. It allows you to retrieve the Unicode code point of the character that comes immediately before a specified index in a string. This method is particularly useful for processing and analyzing characters in a string, especially when dealing with supplementary characters.

codePointBefore Method Syntax

The syntax for the codePointBefore method is as follows:

public int codePointBefore(int index)
  • index: The index of the character before which the Unicode code point is to be returned.

Examples

Getting the Code Point Before a Character

The codePointBefore method can be used to get the Unicode code point of the character immediately before a specified index.

Example

public class CodePointBeforeExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        int codePoint = message.codePointBefore(7);

        System.out.println("Unicode code point before index 7: " + codePoint);
        System.out.println("Character before index 7: " + (char)codePoint);
    }
}

Output:

Unicode code point before index 7: 44
Character before index 7: ,

Handling Supplementary Characters

The codePointBefore method can handle supplementary characters, which are represented by two char values (a surrogate pair) in UTF-16.

Example

public class CodePointBeforeExample {
    public static void main(String[] args) {
        String message = "A\uD835\uDD0A";

        int codePoint = message.codePointBefore(2);

        System.out.println("Unicode code point before index 2: " + codePoint);
    }
}

Output:

Unicode code point before index 2: 119978

Handling Invalid Index

The codePointBefore method will throw an IndexOutOfBoundsException if the specified index is less than 1 or greater than the length of the string.

Example

public class CodePointBeforeExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        try {
            int codePoint = message.codePointBefore(0);
            System.out.println("Unicode code point before index 0: " + codePoint);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: String index out of range: 0

Conclusion

The String.codePointBefore() method in Java is used for retrieving the Unicode code point of the character immediately before a specified index. By understanding how to use this method, you can efficiently process and analyze characters in your Java applications. Whether you are dealing with regular or supplementary characters, or handling potential exceptions, the codePointBefore method provides a reliable solution for these tasks.

Comments