Java StringBuilder charAt() Method

Java StringBuilder charAt() Method | In this blog, we will discuss the charAt() method. The charAt() method is used to return the character at the specific index in the string of character sequence. The charAt() method is a string manipulation method that is present in the StringBuilder class which means it supports single threading and is not synchronized.

The index value passed to the charAt() method should be between 0 and length – 1. If the value exceeds the (length – 1) or is less than 0 then the method throws an IndexOutOfBoundException. This method works very similarly to the String class charAt() method.

The syntax for the charAt() method is as follows:- public char charAt(int index)
Parameter: The index value for which the character needs to be returned.
Return type: Character.
Returns: The character at the specified index.
Exception: Throws IndexOutOfBoundException if the index value is below 0 or exceeds the string length.

An example of the charAt() method is as follows,
Example-1:
String: “Hello”
index: 1
Output: ‘e’

Example-2:
String:- “Hello”
index: -1
Output: IndexOutOfBoundException

Java StringBuilder charAt() Method Example

Let us see some examples of the StringBuilder charAt() method. We will take a StringBuilder object with some string and then call charAt() method.

public class Main {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder();
      sb.append("Java Programming Language");
      System.out.println(sb.charAt(5));
      System.out.println(sb.charAt(10));
   }
}

Output:-

P
a

StringBuilder Java charAt() Method Uses

This program returns each and every character in the given StringBuilder. We will iterate the StringBuilder object, fetch each character, and display the character.

public class Main {
   public static void main(String[] args) {
      StringBuilder string = new StringBuilder("Java");
      System.out.println("String: " + string);
      for (int i = 0; i < string.length(); i++) {
         System.out.print(string.charAt(i) + " ");
      }
   }
}

Output:-

String: Java
J a v a

Exception thrown By Java StringBuilder charAt() Method

The charAt() method of the StringBuilder class throws IndexOutOfBoundException if the passed parameter is negative or greater than or equal to the length of the string. The below programs demonstrate it:-

public class Main {
   public static void main(String[] args) {
      try {
         StringBuilder sb = new StringBuilder("Java");
         char ch = sb.charAt(sb.length());
         System.out.println("Char at position " + sb.length() + " is " + ch);
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
}

Output:-

Exception: java.lang.StringIndexOutOfBoundsException: index 4, length 4

public class Main {
   public static void main(String[] args) {
      try {
         StringBuilder sb = new StringBuilder("Java");
         char ch = sb.charAt(-9);
         System.out.println("Char at position " + sb.length() + " is " + ch);
      } catch (Exception e) {
         System.out.println("Exception: " + e);
      }
   }
}

Output:-

Exception: java.lang.StringIndexOutOfBoundsException: index -9, length 4

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.