Java StringBuilder charAt() Method



The Java StringBuilder charAt() method returns the char value in this sequence at the specified index.

The first char value represents the 0 index, the second char value represents the next index, which is 1, and so on; indexing is similar to an array indexing. The value of the index argument must be greater than or equal to 0 and less than the length of the given sequence.

The charAt() method accepts an integer parameter that represents the index value. If the index value is less than zero or greater than the sequence length, the method throws an exception.

Syntax

Following is the syntax of the Java StringBuilder charAt() method −

public char charAt(int index)

Parameters

  • index − This is the index of the desired char value.

Return Value

This method returns the char value at the specified index.

Example

If the index value is positive and less than the given string length, this method will return the char value at the specified index.

In this program, we are creating a StringBuilder with the value “StringBuilder”. Using the charAt() method we are trying to retrieve the char value at 5th index.

package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("StringBuilder");
      System.out.println("String: " + sb);
      
      // returns the char at index 5th
      int index = 5;
      System.out.println("The index value is: " + index);
      char ch = sb.charAt(index);
      System.out.println("The char value at " + index + " index is: " + ch);
   }
}

Output

On executing the above program, it will produce the following result −

String: StringBuilder
The index value is: 5
The char value at 5 index is: g

Example

If the index value is less than zero or negative, this method throws an IndexOutOfBoundsException

In the following program, we are creating an object of the StringBuilder with the value "Hello World". We are trying to retrieve the char value at -1 index using the charAt() method.

package com.tutorialspoint.StringBuilder;
public class Demo {
   public static void main(String[] args) {
      try {
         
         //create a StringBuilder
         StringBuilder sb = new StringBuilder("Hello World");
         System.out.println("StringBuilder: " + sb);
         
         //declare a integer variable that holds the index value
         int index = -1;
         
         //using the charAt() method
         System.out.println("The char value of the " + index + " index is: " + sb.charAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

StringBuilder: Hello World
java.lang.StringIndexOutOfBoundsException: index -1, length 11
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:351)
	at java.base/java.lang.StringBuilder.charAt(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Demo.main(Demo.java:11)
Exception: java.lang.StringIndexOutOfBoundsException: index -1, length 11

Example

If the index value is greater than the length of the given sequence, this method will throw an IndexOutOfBoundsException

In this example, we are creating a StringBuilder that contains “TutorialsPoint” as an input value. Using the charAt() method, we are trying to retrieve the char value at the index whose value is greater than the string length.

package com.tutorialspoint.StringBuilder;
public class Test {
   public static void main(String[] args) {
      try {
         
         //create a StringBuilder
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         System.out.println("StringBuilder: " + sb);
         
         //declare a integer variable that holds the index value
         int index = 20;
         
         //using the charAt() method
         System.out.println("The char value of the " + index + " index is: " + sb.charAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

StringBuilder: TutorialsPoint
java.lang.StringIndexOutOfBoundsException: index 20, length 14
	at java.base/java.lang.String.checkIndex(String.java:4563)
	at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:351)
	at java.base/java.lang.StringBuilder.charAt(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.Demo.main(Demo.java:11)
Exception: java.lang.StringIndexOutOfBoundsException: index 20, length 14
java_lang_stringbuilder.htm
Advertisements