Java StringBuilder codePointAt() Method

The StringBuilder.codePointAt() method in Java is used to retrieve the Unicode code point value of the character at a specified index within a StringBuilder object. 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
    • Retrieving Code Point by Index
    • Handling IndexOutOfBoundsException
    • Using Code Points for Supplementary Characters
  4. Conclusion

Introduction

The StringBuilder.codePointAt() method is a member of the StringBuilder class in Java. It allows you to access the Unicode code point value of the character at a specified index within the StringBuilder object. This method is particularly useful when dealing with Unicode characters, including supplementary characters that require more than one char value.

codePointAt Method Syntax

The syntax for the codePointAt method is as follows:

public int codePointAt(int index)
  • index: The position of the character whose Unicode code point value is to be retrieved. The index is zero-based, meaning the first character of the sequence is at index 0.

Examples

Retrieving Code Point by Index

The codePointAt method can be used to access the Unicode code point value of characters at specific positions within a StringBuilder.

Example

public class StringBuilderCodePointAtExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        int codePointFirst = sb.codePointAt(0);
        int codePointSeventh = sb.codePointAt(6);
        int codePointLast = sb.codePointAt(sb.length() - 1);

        System.out.println("Code point of first character: " + codePointFirst);
        System.out.println("Code point of seventh character: " + codePointSeventh);
        System.out.println("Code point of last character: " + codePointLast);
    }
}

Output:

Code point of first character: 72
Code point of seventh character: 44
Code point of last character: 33

Handling IndexOutOfBoundsException

Attempting to access an index that is out of bounds will result in an IndexOutOfBoundsException. It's important to ensure that the specified index is within the valid range.

Example

public class StringBuilderCodePointAtExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        try {
            int invalidCodePoint = sb.codePointAt(20); // This will throw an exception
            System.out.println("Code point at index 20: " + invalidCodePoint);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: Index out of bounds. " + e.getMessage());
        }
    }
}

Output:

Error: Index out of bounds. String index out of range: 20

Using Code Points for Supplementary Characters

For supplementary characters (characters outside the Basic Multilingual Plane, BMP), which are represented by a pair of char values (a surrogate pair), the codePointAt method can be used to get the correct code point value.

Example

public class StringBuilderCodePointAtExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World! \uD83D\uDE00"); // Unicode for 😀 (grinning face)

        int codePointEmoji = sb.codePointAt(13); // Index of the supplementary character

        System.out.println("Code point of emoji: " + codePointEmoji);
    }
}

Output:

Code point of emoji: 128512

Conclusion

The StringBuilder.codePointAt() method in Java is used for retrieving the Unicode code point value of characters within a StringBuilder object. By understanding how to use this method, you can efficiently handle and manipulate Unicode characters, including supplementary characters. Whether you need to retrieve code points by index, handle potential exceptions, or work with characters beyond the BMP, the codePointAt method provides a reliable solution for these tasks.

Comments