Java String codePointAt() Method

The String.codePointAt() method in Java is used to return the Unicode code point of the character at 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. codePointAt Method Syntax
  3. Examples
    • Getting the Code Point of a Character
    • Handling Supplementary Characters
    • Handling Invalid Index
  4. Conclusion

Introduction

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

codePointAt Method Syntax

The syntax for the codePointAt method is as follows:

public int codePointAt(int index)
  • index: The index of the character whose Unicode code point is to be returned.

Examples

Getting the Code Point of a Character

The codePointAt method can be used to get the Unicode code point of a character at a specified index.

Example

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

        int codePoint = message.codePointAt(7);

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

Output:

Unicode code point at index 7: 87
Character at index 7: W

Handling Supplementary Characters

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

Example

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

        int codePoint = message.codePointAt(1);

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

Output:

Unicode code point at index 1: 119978

Handling Invalid Index

The codePointAt method will throw an IndexOutOfBoundsException if the specified index is negative or not less than the length of the string.

Example

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

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

Output:

Error: String index out of range: 20

Conclusion

The String.codePointAt() method in Java is used for retrieving the Unicode code point of a character at 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 codePointAt method provides a reliable solution for these tasks.

Comments