StringBuffer is a String peer class that provides a lot of the same functionality as strings. StringBuffer represents growable and writable character sequences, whereas a string represents fixed-length, immutable character sequences. Characters and substrings can be inserted in the middle or appended to the end of a StringBuffer.
Constructors of StringBuffer class :
1. StringBuffer(): It saves 16 characters by not reallocating space.
StringBuffer s = new StringBuffer();
2. StringBuffer( int size): It accepts an integer argument that specifies the buffer’s size.
StringBuffer s = new StringBuffer(20);
3. StringBuffer(String str): It takes a string argument that sets the initial contents of the StringBuffer object and frees up space for 16 more characters without reallocating space.
StringBuffer s = new StringBuffer("studentscoding");
Important methods of StringBuffer class:
Modifier and Type | Method | Description |
---|---|---|
public synchronized StringBuffer | append(String s) | It is used to append the specified string with this string. Like append(char), append(boolean), append(int), append(float), append(double), etc., the append() function has many overloads. |
public synchronized StringBuffer | insert(int offset, String s) | It is used to insert the specified string with this string at the specified position. There are several other overloaded versions of the insert() method, including insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc. |
public synchronized StringBuffer | replace(int startIndex, int endIndex, String str) | It replaces the string from specified startIndex and endIndex. |
public synchronized StringBuffer | delete(int startIndex, int endIndex) | It deletes the string from specified startIndex and endIndex. |
public synchronized StringBuffer | reverse() | it reverses the string. |
public int | capacity() | It returns the current capacity. |
public void | ensureCapacity(int minimumCapacity) | It ensures the capacity is at least equal to the given minimum. |
public char | charAt(int index) | It returns the character at the specified position. |
public int | length() | It returns the length of the string i.e. total number of characters. |
public String | substring(int beginIndex) | It returns the substring from the specified beginIndex. |
public String | substring(int beginIndex, int endIndex) | It returns the substring from the specified beginIndex and endIndex. |
Example: length(),capacity(),append(), reverse(), replace(), ensurecapacity()
import java.io.*;
// Main class
class Demo {
// main driver method
public static void main(String[] args)
{
// Creating adn storing string by creating object of
// StringBuffer
StringBuffer s = new StringBuffer("Coderzpy");
// Getting the length of the string
int p = s.length();
// Getting the capacity of the string
int q = s.capacity();
System.out.println("Length of string "+s+"="
+ p);
System.out.println(
"Capacity of string "+s+"=" + q);
s.append(".com");
System.out.println("Length of string "+s+"="
+ s.length());
System.out.println(
"Capacity of string "+s+"=" + s.capacity());
System.out.println("reverse = "+s.reverse());
s.deleteCharAt(7);
// Returns string after deletion
System.out.println("deleted "+s);
s.replace(5, 8, "are");
System.out.println("replaced with are"+s);
s.ensureCapacity(40);
System.out.println(
"Capacity of string "+s+"=" + s.capacity());
}
}
Output:
Length of string Coderzpy=8
Capacity of string Coderzpy=24
Length of string Coderzpy.com=12
Capacity of string Coderzpy.com=24
reverse = moc.ypzredoC
deleted moc.ypzedoC
replaced with aremoc.yaredoC
Capacity of string moc.yaredoC=50
Note: also read about the Some examples of string class methods
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment