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

By Chaitanya Singh | Filed Under: java

Java StringBuilder setCharAt() method sets a specified character at the given index. This method changes the character sequence represented by StringBuilder 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 StringBuilder 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 StringBuilder 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: Sets a new character in the existing sequence

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

    //set char 'N' at the index 3
    sb.setCharAt(3, 'N');

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

Output:

Java StringBuilder setCharAt() Example Output_1

Example 2: Changes the first and last character of Sequence

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

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

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

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

Output:

Java StringBuilder setCharAt() Example Output_2

Example 3: Specified index is equal to the length of Sequence

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

    //if index is >= sb.length()
    //last char is at index 1
    //index 2 is out of sequence range
    sb.setCharAt(2, 'A');

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

Output:

Java StringBuilder setCharAt() Example Output_3

Recommended Posts

  • Java StringBuilder deleteCharAt()
  • Java StringBuilder getChars()

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