StringBuffer Class
StringBuffer Class
1. StringBuffer objects are mutable, meaning that you can change the
contents of the buffer without creating a new object.
2. The initial capacity of a StringBuffer can be specified when it is
created, or it can be set later with the ensureCapacity() method.
3. The append() method is used to add characters, strings, or other
objects to the end of the buffer.
4. The insert() method is used to insert characters, strings, or other
objects at a specified position in the buffer.
5. The delete() method is used to remove characters from the buffer.
6. The reverse() method is used to reverse the order of the characters
in the buffer.
Java
Output
Hello world
1. append() method
Example:
Java
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // now original string is changed
System.out.println(sb);
}
}
Output
Hello Java
2. insert() method
The insert() method inserts the given string with this string at the
given position.
Example:
Java
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java");
// Now original string is changed
System.out.println(sb);
}
}
Output
HJavaello
3. replace() method
The replace() method replaces the given string from the specified
beginIndex and endIndex-1.
Example:
Java
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}
Output
HJavalo
4. delete() method
The delete() method of the StringBuffer class deletes the string from
the specified beginIndex to endIndex-1.
Example:
Java
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}
Output
Hlo
5. reverse() method
Example:
Java
import java.io.* ;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}
Output
olleH
6. capacity() method
Example:
Java
import java.io.*;
class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // default 16
sb.append("Hello");
System.out.println(sb.capacity()); // now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());
// Now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
Output
16
16
34
Note: Besides that, all the methods that are used in the String
class can also be used. These auxiliary methods are as follows:
specified index.
void
In this method, the
getChars(int
characters are copied public void getChars(int
srcBegin, int
from this sequence srcBegin, int srcEnd, char[]
srcEnd, char[]
into the destination dst, int dstBegin)
dst, int character array dst.
dstBegin)
end) sequence.
Java
// Main class
class GFG {
Output
Java
// Main class
class GFG {
// Returns GeeksforGeeks
System.out.println(s);
s.append(1);
// Returns GeeksforGeeks1
System.out.println(s);
}
}
Output
GeeksforGeeks
GeeksforGeeks1
Example 3: insert()
Here, the index specifies the index at which point the string will be
inserted into the invoking StringBuffer object.
Java
// Main class
class GFG {
s.insert(0, 5);
// Returns 5GeeksforGeeks
System.out.println(s);
s.insert(3, true);
// Returns 5GetrueeksforGeeks
System.out.println(s);
s.insert(5, 41.35d);
// Returns 5Getr41.35ueeksforGeeks
System.out.println(s);
s.insert(8, 41.35f);
// Returns 5Getr41.41.3535ueeksforGeeks
System.out.println(s);
Output
GeeksforGeeks
5GeeksforGeeks
5GetrueeksforGeeks
5Getr41.35ueeksforGeeks
5Getr41.41.3535ueeksforGeeks
5Gpawanetr41.41.3535ueeksforGeeks
Example 4: reverse( )
Java
// Main class
class GFG {
// Returns "skeeGrofskeeG"
System.out.println(s);
}
}
Output
skeeGskeeG
Syntax:
Java
s.delete(0, 5);
// Returns forGeeks
System.out.println(s);
s.deleteCharAt(7);
// Returns forGeek
System.out.println(s);
}
}
Output
forGeeks
forGeek
Example 6: replace()
Syntax:
Example
Java
// Main class
class GFG {
// Returns GeeksareGeeks
System.out.println(s);
}
}
Output
GeeksareGeeks
Feeling lost in the vast world of Backend Development? It's time for a
change! Join our Java Backend Development - Live Course and embark
on an exciting journey to master backend development efficiently and
on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Previous Next
Similar Reads
equals() on String and StringBuffer objects in Java
10 Best Free Social Media Management and Marketing Apps for Android - 2024
10 Best Customer Database Software of 2024
How to Delete Whatsapp Business Account?
Discord vs Zoom: Select The Efficienct One for Virtual Meetings?
30 OOPs Interview Questions and Answers (2024)
A-143, 9th Floor, Sovereign Corporate
Tower, Sector-136, Noida, Uttar Pradesh -
201305
Company Explore
About Us Hack-A-Thons
Legal GfG Weekly Contest
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
Advertise with us GeeksforGeeks Videos
GFG Corporate Solution Geeks Community
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive