Java StringBuffer charAt() Method



The Java StringBuffer charAt() method is used to get the char value in this sequence at the specified index. As the StringBuffer 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 StringBuffer 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

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 StringBuffer charAt() method. Here, we are declaring a StringBuffer 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;

import java.lang.*;

public class StringBufferDemo {

   public static void main(String[] args) {

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

      // returns the char at index 4
      System.out.println("character = " + buff.charAt(4));
   
      buff = new StringBuffer("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

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.

import java.lang.*;

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

      StringBuffer sb = new StringBuffer("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

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

import java.lang.*;

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

      // Creating a StringBuffer object and Initializing it
      StringBuffer sb = new StringBuffer("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.StringBuffer.charAt(StringBuffer.java:243)
        at StringBufferCharAt.main(StringBufferCharAt.java:10)

Example

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

import java.lang.*;

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

      // Creating a StringBuffer object and Initializing it
      StringBuffer sb = new StringBuffer("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.StringBuffer.charAt(StringBuffer.java:243)
        at StringBufferCharAt.main(StringBufferCharAt.java:13)
java_lang_stringbuffer.htm
Advertisements