beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

StringBuilder.charAt() Method

By Chaitanya Singh | Filed Under: Java.lang.StringBuilder

Description

public char charAt(int index): This method returns the character present at the specified index in the StringBuilder instance. The first char of StringBuilder is at index 0, second one is at index 1 and so on. The index argument must be greater than or equal to 0, and less than the length of the sequence, otherwise it would throw IndexOutOfBoundsException

Example

This example shows, how to find the character of a particular index in a StringBuilder instance.

import java.lang.StringBuilder;
class StringBuilderCharAt{ 
  public static void main(String[] args) {
     StringBuilder sb = new StringBuilder("TEST STRING");
 
     System.out.println("String is: "+sb);
 
     for(int i=0; i<sb.length(); i++){
         System.out.println("Character at index "+i+
         " is: "+sb.charAt(i));
     }
  }
}

Output:

String is: TEST STRING
Character at index 0 is: T
Character at index 1 is: E
Character at index 2 is: S
Character at index 3 is: T
Character at index 4 is:  
Character at index 5 is: S
Character at index 6 is: T
Character at index 7 is: R
Character at index 8 is: I
Character at index 9 is: N
Character at index 10 is: G

Enjoyed this post? Try these related posts

  1. StringBuilder.append(boolean b) Method
  2. StringBuilder.append(char c) Method
  3. StringBuilder.capacity() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap