StringBuffer Class in Java:
The StringBuffer class in java creates a string of flexible (variable) length. The string created using the StringBuffer class can be modified in terms of both length and content. These classes are contained in the package,java.lang.There are three constructor available in StringBuffer.They are:
- StringBuffer()
- StringBuffer(int lenght)
- StringBuffer(String str)
StringBuffer():
It creates an empty StringBuffer. Its capacity is 16-character. This is a default constructor. To create an empty as follows:
StringBuffer buffername=new StringBuffer();
where,
- StringBuffer and new are keywords
- Buffername is a name of the StringBuffer
Example:
StringBuffer sb=new StringBuffer();
StringBuffer(int length):
It creates an empty StringBuffer. Its capacity is up to n number of characters.To create an empty StringBuffer based on the length of the characters.
StringBuffer buffername= new StringBuffer();
Example:
StringBuffer sb=new StringBuffer(10);
StringBuffer(String str):
It creates a string buffer from string objects.
StringBuffer Methods:
Some commonly used methods of StringBuffer class are:1.length()
The length() method return the number of characters in the string buffer object.The general format is:StringBufferobject.length()
Example:
class bufferlen
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Varun");
int n=sb.length();
System.out.println("The length of the string buffer is:"+n);
}
}
The output is:
The length of the string buffer is:5
2.capacity()
The capacity() method returns the allocated capacity of a string buffer object.The general format is:StringBufferObject.capacity();
Example:
class buffercap
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("xavier");
int n=sb.capacity();
System.out.println("The capacity of the String buffer object is:"+n);
}
}
The output is :
22
3.setLength()
The setLength() method is used to set the length of the buffer string.The general form is:StringBufferobject.setLength(int length)
Example:
class bufferlen
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("xavier");
sb.setLength(3);
System.out.println(sb);
}
}
The output is :
xav
4.setCharAt()
The setCharAt() method is used to set the character from the string buffer.It is similar to replace() method of the String class.The general format is:StringBufferobject.setCharAt(index,char);
Example:
class buffersetchar
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("xavier");
sb.setCharAt(1,'o');
System.out.println("The String is:"+sb);
}
}
The output is :
xovier
5.insert()
The insert() method is used to insert the string from the StringBuffer object.The general format is:StringBufferobject.insert(index,string)
Example:
class insertdemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("john");
sb.insert(7,"abraham");
System.out.println(sb);
}
}
The output is :
john abraham
6.append()
The append() method is used to insert the string at the end of the StringBuffer object.The general format is:StringBufferobject.append(String s)
Example:
class append
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("object oriented");
String str1="programming"
sb.append(str1);
System.out.println(sb);
}
}
The output is :
object orientedprogramming
7.reverse()
The reverse() method is used to reverse the string in the StringBuffer object.The general format is:StringBufferobject.reverse()
Example:
class reversedemo
{
public static void main(String args[])
{
StringBuffer sb=new StringBuffer("Siddharth");
sb.reverse();
System.out.println(sb);
}
}
The output is :htrahddis