Java String codePointAt() Method Example

The String.codePointAt() method in Java is used to return the Unicode code point at a specified index within 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
    • Basic Example
    • Using codePointAt with Supplementary Characters
  4. Conclusion

Introduction

The String.codePointAt() method is part of the String class in Java. It allows you to retrieve the Unicode code point of a character at a specified index. This is particularly useful when dealing with characters outside the Basic Multilingual Plane (BMP), as those characters are represented by a pair of char values (a surrogate pair) in a String.

codePointAt Method Syntax

The syntax for the codePointAt method is as follows:

public int codePointAt(int index)
  • index: The index of the character from which to get the Unicode code point.
  • Returns: The Unicode code point value of the character at the specified index.

Examples

Basic Example

In this example, we'll use the codePointAt method to get the Unicode code point of a character at a specific index in a String.

Example

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

        // Get the Unicode code point of the character at index 1
        int codePoint = str.codePointAt(1);

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

Output:

Character at index 1: e
Unicode code point at index 1: 101

Using codePointAt with Supplementary Characters

Supplementary characters are those characters that are outside the BMP (Unicode code points from U+10000 to U+10FFFF) and are represented by a pair of char values in a String.

Example

public class CodePointAtSupplementaryExample {
    public static void main(String[] args) {
        // A string containing a supplementary character
        String str = "A\uD835\uDD46Z";

        // Get the Unicode code point of the supplementary character at index 1
        int codePoint = str.codePointAt(1);

        System.out.println("Character at index 1 and 2: " + str.charAt(1) + str.charAt(2));
        System.out.println("Unicode code point at index 1: " + codePoint);
    }
}

Output:

Character at index 1 and 2: 𝕆
Unicode code point at index 1: 120134

Handling Invalid Indices

If the specified index is outside the range of the string, an IndexOutOfBoundsException is thrown.

Example

public class CodePointAtInvalidIndexExample {
    public static void main(String[] args) {
        String str = "Hello";

        try {
            // Attempt to get the Unicode code point at an invalid index
            int codePoint = str.codePointAt(10);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException: " + e.getMessage());
        }
    }
}

Output:

IndexOutOfBoundsException: String index out of range: 10

Conclusion

The String.codePointAt() method in Java provides a way to retrieve the Unicode code point of a character at a specified index in a String. This method is particularly useful when working with supplementary characters, which are represented by surrogate pairs. By understanding how to use this method, you can effectively handle and process Unicode characters in your Java applications.


Comments