- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change a single character in a Java StringBuffer object in Java
In order to change a single character in a StringBuffer object in Java, we use the setCharAt() method. The setCharAt() method sets the character at the index specified as a parameter to another character whose value is passed parameter of the setCharAt() method. The method sets a new character sequence with the only change being the character passed as the parameter at the specified index.
Declaration - The java.lang.StringBuffer.setCharAt() method is declared as follows−
public void setCharAt(int index, char ch)
If index is greater than the length of the StringBuffer object or is negative, an IndexOutOfBoundsException is generated.
Let us see a program to change a single character in a StringBuffer object.
Example
public class Example { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Hello World"); sb.setCharAt(7,'a'); // setting character to a at the 7th index System.out.println(sb); } }
Output
Hello Warld
- Related Articles
- Remove a character from a Java StringBuffer object
- Change the length of the StringBuffer Object in Java
- Change the default capacity of the StringBuffer Object in Java
- Remove characters in a range from a Java StringBuffer Object
- Get the length of a StringBuffer Object in Java
- Set the capacity value for a StringBuffer object in Java
- Call append() method to construct a StringBuffer object in Java
- Reverse the sequence of characters in a StringBuffer Object in Java
- What is the difference between a String object and a StringBuffer object in java?
- Get the capacity of the StringBuffer Object in Java
- Create a StringBuffer object using a reference stored in a variable of type String in Java
- Append a single character to a string or char array in java?
- How to read a single character using Scanner class in Java?
- Is StringBuffer final in Java?
- Different methods to append a single character to a string or char array in Java

Advertisements