Java.lang.StringBuffer.charAt() Method

Advertisements


Description

The java.lang.StringBuffer.charAt() method returns the char value in this sequence at the specified index. The first char value is at index 0, the next at index 1, and so on, as in array indexing.

The index argument must be greater than or equal to 0, and less than the length of this sequence.

Declaration

Following is the declaration for java.lang.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.

Exception

  • IndexOutOfBoundsException -- if index is negative or greater than or equal to length().

Example

The following example shows the usage of java.lang.StringBuffer.charAt() method.

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));
  }
}

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

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



Advertisements
Advertisements