Way2Java

charAt() StringBuffer Example

To manipulate string, java.lang.StringBuffer class comes with many methods, otherwise which will take long laborious coding as in C/C++. One such method is charAt() StringBuffer that returns the character at the specified index number passed as argument to the method.

What Java API says about charAt() StringBuffer:

Following charAt() StringBuffer example illustrates.
public class Demo
{
  public static void main(String args[])
  {						
    StringBuffer sb1 = new StringBuffer("ABCDEF");
    char ch = sb1.charAt(2);                    // index number starts from 0
    System.out.println("Character present at index number 2: " + ch);  // C

    System.out.println("Character present at index number 10: " + sb1.charAt(10));
  }
}



Output screenshot on charAt() StringBuffer Example

Giving index number greater than the length of the buffer (number of characters present in the buffer) throws StringIndexOutOfBoundsException.

Pass your comments and suggestions on this tutorial "charAt() StringBuffer Example".