StringBuilder charAt() in Java with Examples Last Updated : 15 Oct, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output: StringBuilder Object contains = Geek Character at Position 1 in StringBuilder = e Example 2: Java // 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); } } } Output: 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 // 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); } } } Output: 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) Comment More infoAdvertise with us Next Article StringBuilder replace() in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Functions Java-StringBuilder Practice Tags : Java Similar Reads StringBuilder deleteCharAt() in Java with Examples The deleteCharAt(int index) method of StringBuilder class remove the character at the given index from String contained by StringBuilder. This method takes index as a parameter which represents the index of char we want to remove and returns the remaining String as StringBuilder Object. This StringB 3 min read StringBuilder codePointAt() in Java with Examples 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 val 4 min read StringBuilder length() in Java with Examples The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o 2 min read StringBuilder codePointCount() in Java with Examples The codePointCount() method of StringBuilder class returns the number of Unicode code points in the specified text range in String contained by StringBuilder. This method takes two indexes as a parameter- first beginIndex which represents index of the first character of the text range and endIndex w 3 min read StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read StringBuilder codePointBefore() in Java with Examples The codePointBefore() method of StringBuilder class takes an index as a parameter and returns the "Unicode number" of the character before the specified index in String contained by StringBuilder. The index refers to char values (Unicode code units) and the value of index must lie between 0 to lengt 2 min read Like