java.lang.StringBuffer charAt(int index)

Description :

This java tutorial shows how to use the charAt(int index) method of StringBuffer class under java.lang package.

The charAt(int index)  method of StringBuffer class 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 following rules should be followed in dealing with this method:

  • 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.

Method Syntax :

public char charAt(int index)

Parameter Input :

DataType Parameter Description
int index the position of the required character

Method Returns :

The charAt(int index) method simply returns the char value at the specified index as method argument.

Compatibility Version :

Requires Java 1.0 and up

Java Code Example :

This java example source code demonstrates the use of charAt(int index) method of StringBuffer class. Initially the code assigns a string “java-tutorial” as initial contents of the string buffer. Then we extracted and print the 3rd index on the buffer using the charat method.

Sample Output :

Running the charAt(int index) method example source code of StringBuffer class will give you the following output

Exception Scenario :

IndexOutOfBoundsException – will be thrown if either index is negative or greater than or equal to length().

Similar Method :

  • N/A

Suggested Reading List :

References :