Java StringBuilder charAt() Method



The Java StringBuilder charAt() method is used to get the char value in this sequence at the specified index. As the StringBuilder is same as a String (but modifiable), indexing is also similar in it. The first char value is at index 0, the next at index 1, and so on, as in String/Array indexing.

Note

  • The index argument must be greater than or equal to 0, and less than the length of this sequence.
  • If the char value specified by the index is a surrogate, the surrogate value is returned.

Syntax

Following is the syntax for 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: Getting char at given Index

When we pass the index as an argument to the method, the character at that index would be returned.

The following example shows the usage of Java StringBuilder charAt() method. Here, we are declaring a StringBuilder object and initializing it with a sequence of characters. The method takes an index as the argument to find the character present at that index.

package com.tutorialspoint;


public class StringBuilderDemo {

   public static void main(String[] args) {

      StringBuilder buff = new StringBuilder("Tutorials Point");
      System.out.println("buffer = " + buff);

      // returns the char at index 4
      System.out.println("character = " + buff.charAt(4));
   
      buff = new StringBuilder("amrood admin ");
      System.out.println("buffer = " + buff);
      
      // returns the char at index 6, whitespace gets printed here
      System.out.println("character = " + buff.charAt(6));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

buffer = Tutorials Point
character = r
buffer = amrood admin
character =  

Example: Getting char at given index

Another example to retrieve a character present at an index is given below. Here, we are performing an arithmetic addition and its result is passed as an argument to the method.


public class StringBuilderCharAt {
   public static void main(String args[]) {

      StringBuilder sb = new StringBuilder("Tutorialspoint");

      int id1 = 6;
      int id2 = 7;

      System.out.println("The character at " + (id1 + id2) + " index is: " + sb.charAt(id1 + id2));
   }
}

Output

Let us compile and run the program above, the result is obtained as follows −

The character at 13 index is: t

Example: Facing Exception while getting char at Negative Index

But when we pass a negative index as the argument to the method, a StringIndexOutOfBounds Exception is thrown.


public class StringBuilderCharAt {
   public static void main(String args[]) {

      // Creating a StringBuilder object and Initializing it
      StringBuilder sb = new StringBuilder("Tutorialspoint");

      // declare and assign the index value to an int variable
      int id = -4;

      // print the output
      System.out.println("The character at " + id + " index is: " + sb.charAt(id)); //throws exception
   }
}

Exception

If we try to compile and run the program above, the method throws a StringIndexOutOfBoundsException −

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index -4 out of bounds for length 14
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.lang.String.checkIndex(String.java:4570)
        at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:358)
        at java.base/java.lang.StringBuilder.charAt(StringBuilder.java:243)
        at StringBuilderCharAt.main(StringBuilderCharAt.java:10)

Example: Facing Exception while getting char with invalid Index

In another case, if we pass the index value that is greater than the input sequence length, the method also throws a StringIndexOutOfBounds Exception.


public class StringBuilderCharAt {
   public static void main(String args[]) {

      // Creating a StringBuilder object and Initializing it
      StringBuilder sb = new StringBuilder("Tutorialspoint");

      // declare and assign the index value to an int variable
      int id = 15;

      // print the output
      System.out.println("The character at " + id + " index is: " + sb.charAt(id)); //throws exception
   }
}

Output

If we try to compile and run the program above, the method throws a StringIndexOutOfBoundsException −

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 14
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
        at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
        at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.lang.String.checkIndex(String.java:4570)
        at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:358)
        at java.base/java.lang.StringBuilder.charAt(StringBuilder.java:243)
        at StringBuilderCharAt.main(StringBuilderCharAt.java:13)
java_lang_stringbuilder.htm
Advertisements