Open In App

StringBuilder codePointAt() in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The codePointAt(int index) method of StringBuilder class takes an index as a parameter and returns a character unicode point at that index in String contained by StringBuilder or we can say charPointAt() method returns the “unicode number” of the character at that index. The index refers to char values (Unicode code units) and the value of index must be lie between 0 to length-1.

If the char value present at the given index lies in the high-surrogate range, the following index is less than the length of this sequence, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.

Syntax:

public int codePointAt(int index)

Parameters: This method accepts one int type parameter index which represents index of the character whose unicode value to be returned.

Return Value: This method returns “unicode number” of the character at the specified position.

Exception: This method throws IndexOutOfBoundsException when index is negative or greater than or equal to length().

Below programs demonstrate the codePointAt() method of StringBuilder Class:

Example 1:




// Java program to demonstrate
// the codePointAt() method
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        StringBuilder str = new StringBuilder();
  
        // add the String to StringBuilder Object
        str.append("Geek");
  
        // get unicode of char at position 1
        int unicode = str.codePointAt(1);
  
        // print the result
        System.out.println("StringBuilder Object"
                           + " contains = " + str);
        System.out.println("Unicode of Character"
                           + " at Position 1 "
                           + "in StringBuilder = "
                           + unicode);
  
        // get unicode of char at position 3
        unicode = str.codePointAt(3);
  
        // print the result
        System.out.println("Unicode of Character "
                           + "at Position 3 "
                           + "in StringBuilder = "
                           + unicode);
    }
}


Output:

StringBuilder Object contains = Geek
Unicode of Character at Position 1 in StringBuilder = 101
Unicode of Character at Position 3 in StringBuilder = 107

Example 2:




// Java program to demonstrate
// the codePointAt() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("WelcomeGeeks");
  
        // print string
        System.out.println("String is " + str.toString());
  
        // loop through string and print every Character
        for (int i = 0; i < str.length(); i++) {
  
            // get char at position i
            char ch = str.charAt(i);
  
            // get unicode of char at position i
            int unicode = str.codePointAt(i);
  
            // print char and Unicode
            System.out.println("Unicode of Char " + ch
                               + " at position " + i
                               + " is " + unicode);
        }
    }
}


Output:

String is WelcomeGeeks
Unicode of Char W at position 0 is 87
Unicode of Char e at position 1 is 101
Unicode of Char l at position 2 is 108
Unicode of Char c at position 3 is 99
Unicode of Char o at position 4 is 111
Unicode of Char m at position 5 is 109
Unicode of Char e at position 6 is 101
Unicode of Char G at position 7 is 71
Unicode of Char e at position 8 is 101
Unicode of Char e at position 9 is 101
Unicode of Char k at position 10 is 107
Unicode of Char s at position 11 is 115

Example 3: To demonstrate IndexOutOfBoundsException




// Java program demonstrate
// IndexOutOfBoundsException thrown by
// the codePointAt() Method.
  
class GFG {
    public static void main(String[] args)
    {
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("WelcomeGeeks");
  
        try {
  
            // get char at position out of range of index
            int i = str.codePointAt(str.length());
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.StringIndexOutOfBoundsException: 
String index out of range: 12

Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#codePointAt(int)



Last Updated : 15 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads