Java String codePointAt() Method Example

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

Introduction

TheString.codePointAt() method is part of theString 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 ofchar values (a surrogate pair) in aString.

codePointAt Method Syntax

The syntax for thecodePointAt 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 thecodePointAt method to get the Unicode code point of a character at a specific index in aString.

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: eUnicode code point at index 1: 101

UsingcodePointAt 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 ofchar values in aString.

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, anIndexOutOfBoundsException 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

TheString.codePointAt() method in Java provides a way to retrieve the Unicode code point of a character at a specified index in aString. 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