Java StringBuffer codePointAt() Method

The StringBuffer.codePointAt() method in Java is used to return the Unicode code point at a specified index within the StringBuffer 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
    • Getting Code Point at a Specific Index
    • Handling Out of Bounds
  4. Conclusion

Introduction

The codePointAt() method is a member of the StringBuffer class in Java. It allows you to retrieve the Unicode code point value of the character at a specified position within the StringBuffer. This is useful for working with characters outside the Basic Multilingual Plane (BMP), which require two char values (a surrogate pair) to be represented.

codePointAt Method Syntax

The syntax for the codePointAt method is as follows:

public synchronized int codePointAt(int index)

Parameters:

  • index - an integer specifying the index of the character whose Unicode code point is to be returned.

Returns:

  • The Unicode code point value of the character at the specified index.

Throws:

  • IndexOutOfBoundsException - if the index argument is negative or not less than the length of this sequence.

Examples

Getting Code Point at a Specific Index

The codePointAt method can be used to access the Unicode code point at a particular index in a StringBuffer object.

Example

public class StringBufferCodePointAtExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello, World!");

        // Get the Unicode code point at index 7
        int codePoint = sb.codePointAt(7);

        // Print the Unicode code point
        System.out.println("Unicode code point at index 7: " + codePoint);

        // Print the character at index 7
        System.out.println("Character at index 7: " + (char) codePoint);
    }
}

Output:

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

Handling Out of Bounds

Attempting to access an index outside the bounds of the StringBuffer will result in an IndexOutOfBoundsException.

Example

public class StringBufferCodePointAtExample {
    public static void main(String[] args) {
        // Create a StringBuffer object with initial content
        StringBuffer sb = new StringBuffer("Hello");

        try {
            // Attempt to get the Unicode code point at an out-of-bounds index
            int codePoint = sb.codePointAt(10);
        } catch (IndexOutOfBoundsException e) {
            // Handle the exception
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

Error: String index out of range: 10

Conclusion

The StringBuffer.codePointAt() method in Java provides a way to retrieve the Unicode code point value of the character at a specified index within a StringBuffer object. By understanding how to use this method, you can work with Unicode characters, including those outside the Basic Multilingual Plane, more effectively. This method is particularly useful for applications that need to handle a wide range of characters and symbols in various languages.

Comments