The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1.
Syntax:
public char charAt(int index)
Parameters: This method accepts one int type parameter index which represents index of the character to be returned.
Return Value: This method returns 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 charAt() method of StringBuilder Class:
Example 1:
// Java program to demonstrate // the charAt() 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 char at position 1 char ch = str.charAt(1); // print the result System.out.println("StringBuilder Object" + " contains = " + str); System.out.println("Character at Position 1" + " in StringBuilder = " + ch); } } |
StringBuilder Object contains = Geek Character at Position 1 in StringBuilder = e
Example 2:
// Java program demonstrate // the charAt() 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); // print char System.out.println("Char at position " + i + " is " + ch); } } } |
String is WelcomeGeeks Char at position 0 is W Char at position 1 is e Char at position 2 is l Char at position 3 is c Char at position 4 is o Char at position 5 is m Char at position 6 is e Char at position 7 is G Char at position 8 is e Char at position 9 is e Char at position 10 is k Char at position 11 is s
Example 3: To demonstrate IndexOutOfBoundException
// Java program to demonstrate // IndexOutOfBound exception thrown by the charAt() Method. class GFG { public static void main(String[] args) { try { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // get char at position i char ch = str.charAt(str.length()); // print char System.out.println("Char at position " + str.length() + " is " + ch); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 12
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#charAt(int)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- CharBuffer charAt() methods in Java with Examples
- Java String charAt() method with example
- StringBuilder Class in Java with Examples
- StringBuilder codePointAt() in Java with Examples
- StringBuilder append() Method in Java With Examples
- StringBuilder delete() in Java with Examples
- StringBuilder codePointCount() in Java with Examples
- StringBuilder capacity() in Java with Examples
- StringBuilder codePointBefore() in Java with Examples
- StringBuilder deleteCharAt() in Java with Examples
- StringBuilder ensureCapacity() in Java with Examples
- StringBuilder getChars() in Java with Examples
- StringBuilder length() in Java with Examples
- StringBuilder offsetByCodePoints() method in Java with Examples
- StringBuilder replace() in Java with Examples
- StringBuilder setCharAt() in Java with Examples
- StringBuilder reverse() in Java with Examples
- StringBuilder setLength() in Java with Examples
- StringBuilder subSequence() in Java with Examples
- StringBuilder substring() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

