BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuffer charAt()

By Chaitanya Singh | Filed Under: java

Java StringBuffer charAt() method returns the character present at the given index. In this tutorial, we will discuss the charAt() method with examples.

Syntax of charAt() method

sb.charAt(0); //returns the first char
sb.charAt(sb.length()-1); //returns the last char

Here, sb is an object of StringBuffer class.

charAt() Description

public char charAt(int index): This method returns the character present at the specified index. The first char of StringBuffer is at index 0, second one is at index 1 and so on.

charAt() Parameters

The charAt() method of Java StringBuffer class takes a single parameter:

  • index: The character present at this index is returned by the charAt() method

The index argument must be greater than or equal to 0, and less than the length of the sequence, otherwise it would throw IndexOutOfBoundsException

charAt() Return Value

  • Returns the character present at the index that we pass as an argument to the charAt() method.

Example 1: Print all characters of a string

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello");
    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:

Java StringBuffer charAt() Output 1

Example 2: Print first and last character of a string

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Hello");
    System.out.println("String is: "+sb);

    //first character of the sequence
    System.out.println("First char: "+sb.charAt(0));
    //first character of the sequence
    System.out.println("Last char: "+sb.charAt(sb.length()-1));
  }
}

Output:

Java StringBuffer charAt() Output 2

Example 3: Print characters at given index

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("BeginnersBook");
    System.out.println("Char at index 0: "+sb.charAt(0));
    System.out.println("Char at index 5: "+sb.charAt(5));
    System.out.println("Char at index 12: "+sb.charAt(12));
  }
}

Output:

Java StringBuffer charAt() Output 3

Example 4: Given index is negative

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Text");
    //negative index in charAt()
    System.out.println(sb.charAt(-5));
  }
}

Output:

Java StringBuffer charAt() Output 4

Example 5: Index is greater than or equal to the length of string

class JavaExample{
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("hello");
    //The string 'hello' has index from 0 to 4
    //so no char present at the index 5
    System.out.println(sb.charAt(5));
  }
}

Output:

Java StringBuffer charAt() Output 5

Recommended Posts

  • Java StringBuffer ensureCapacity()
  • Java StringBuffer capacity()
  • Java StringBuffer toString()
  • Java StringBuffer getChars()
❮ Java StringBuffer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap