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 setCharAt()

By Chaitanya Singh | Filed Under: java

Java StringBuffer setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuffer object, as it replaces the existing char with new char. In this tutorial, we will discuss setCharAt() method with examples.

Syntax of setCharAt() method

sb.setCharAt(2, 'A'); //changes the char at index 2 with char 'A'

Here, sb is an object of StringBuffer class.

setCharAt() Description

public void setCharAt(int index, char ch): Sets the specified char ch at the given index in this char sequence. The length of the StringBuffer object remains unchanged.

setCharAt() Parameters

This method takes two parameters:

  • int index: Specifies the position of a character in the sequence that needs to be replaced with new char.
  • char ch: Represents a char that replaces the character at given index.

setCharAt() Return Value

  • It does not return anything.
  • It throws IndexOutOfBoundsException, if specified index < 0 or >= sb.length().

Example 1: Set a new char in the given sequence

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

    //set char 'H' at the index 5
    sb.setCharAt(5, 'H');

    System.out.println("New Sequence: "+sb);
  }
}

Output:

Java StringBuffer setCharAt() Output1

Example 2: Change first and last character

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

    //set first char as 'C'
    sb.setCharAt(0, 'C');

    //set last char as 'l'
    sb.setCharAt(sb.length()-1, 'l');

    System.out.println("New Sequence: "+sb);
  }
}

Output:

Java StringBuffer setCharAt() Output2

Example 3: Specified index is out of range

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

    //if index is >= sb.length()
    //last char is at index 3
    //index 4 is out of sequence range
    sb.setCharAt(4, 'L');

    System.out.println("New Sequence: "+sb);
  }
}

Output:

Java StringBuffer setCharAt() Output3

Recommended Posts

  • Java StringBuffer codePointAt()
  • Java StringBuffer offsetByCodePoints()
  • Java StringBuffer charAt()
  • Java StringBuffer ensureCapacity()
❮ 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