Home » Java programming language

Java StringBuffer char charAt(int index) method with Example

Java StringBuffer char charAt(int index) method: Here, we are going to learn about the char charAt(int index) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on June 29, 2019

StringBuffer Class char charAt(int index)

  • This method is available in package java.lang.StringBuffer.charAt(int index).
  • This method is used to return the character value at the specified index in the method.
  • This method raises IndexOutOfBoundsException if the given index is negative or greater than the length of the StringBuffer object.

Syntax:

    char charAt(int index){
    }

Parameter(s):

We can pass only one object in the method of the StringBuffer i.e index.

Return value:

The return type of this method is char that means this method return a character at the given index.

Java program to demonstrate example of charAt(int index) method

import java.lang.StringBuffer;

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

        StringBuffer sb = new StringBuffer("Java is a programming language : ");

        // use charAt(int index) it will return character at the specified index

        // Display result after implementing charAt(2)

        System.out.println("The result will be after implementing method charAt(2) is :" + sb.charAt(2));

        sb = new StringBuffer("Java Support OOPS Concept");

        // use charAt(int index) it will return character at the specified index

        // Display result after implementing charAt(5)

        System.out.println("The result will be after implementing method charAt(5) is :" + sb.charAt(5));
    }
}

Output

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after implementing method charAt(2) is :v
The result will be after implementing method charAt(5) is :S



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.