The StringBuffer is a class which is alternative to the String class. But StringBuffer class is more flexible to use than the String class.
StringBuffer Class
The
StringBuffer is a class which is alternative to the String class. But
StringBuffer class is more flexible to use than the String class. That means,
using StringBuffer we can insert some components to the existing string or
modify the existing string but in case of String class once the string is
defined then it remains fixed. The StringBuffer and the StringBuilder are
almost one and the same. The StringBuilder or StringBuffer have three
constructors and 30 methods. Following are some simple methods used for
StringBuffer –
Ex. 4.11.1: Write a Java Program
illustrating the difference between the capacity and length function of
StringBuffer.
Sol. :
Java Program [StringBuffDemo1.java]
public
class StringBuffDemo1{
public
static void main(String args[])
{
StringBuffer
str=new StringBuffer("Java");
System.out.println("The
String Buffer is "+str);
System.out.println("The
length is "+str.length());
System.out.println("The
capacity is"+ str.capacity());-
}
}
Output
F:\test>javac
StringBuffDemo1.java
F:\test>java
StringBuffDemo1
The
String Buffer is Java
The
length is 4
The
capacity is 20
Program Explanation
Using
the StringBuffer/StringBuilder class, we can create a buffer of characters. The
length function returns the number of characters in the string and the capacity
function returns the number of characters in the string +16 additional
characters.
Ex. 4.11.2: Write a Java program,
to convert the string 'Great' to a new string 'God'.
Sol. :
public
class StringBuffDemo2{
public static
void main(String args[])
{
StringBuffer
str=new StringBuffer("Great");
System.out.println("The
String is: "+str);
str.setCharAt(1,'o');'
str.setCharAt(2,'d');
str.setLength(3);
System.out.println("Now
the String is: "+str);
}
}
Output
F:\test>javac
StringBuffDemo2.java
F:\test>java
StringBuffDemo2
The
String is: Great
Now the
String is: God
Program Explanation
In above
program, we have to replace certain characters by new characters and the length
of the string needs to be changed. For that purpose, there are two methods
defined in StringBuffer and those are - setCharAt and setLength.
The
setCharAt method, replaces the desired character by a new character.
setChar(1,'o');
Similarly,
there is a setLength function which sets the length of the string. The string
'Great' has a length 5. To convert this string to a new string say 'God', the
length is set to 3. The above given output simplifies the program.
Ex. 4.4.3 Write a simple Java
program to append the string "God" by the string "is Great"
and then append an exclamation mark to the complete string.
Sol.:
Java Program [StringBuffDemo3.java]
public
class StringBuffDemo3{
public
static void main(String args[])
{
StringBuffer
str=new StringBuffer("God");
System.out.println("Initially
the String is: "+str);
str.append("
is Great");
str.append("!");
System.out.println("Now
the String is: "+str);
}
}
Output
F:\test>javac
StringBuffDemo3.java
F:\test>java
StringBuffDemo3
Initially
the String is: God
Now the
String is: God is Great!
Program Explanation
The append
method is used to insert the string at the end of a given string. In case of
String class the + operator is used to append the string to the existing
string. Using append method we can append character string, numeric value or
any object.
Ex. 4.11.4 Write a Java program
to insert a string "FRIEND" in the given string "----in need is
a in deed".
Sol.:
Java Program[StringBuffDemo4.java]
public
class StringBuffDemo4{
public
static void main(String args[])
{
StringBuffer
str=new StringBuffer(" in need is a in deed");
StringBuffer
new_str=new StringBuffer(" FRIEND");
System.out.println("Initially
the String is: "+str);
str.insert(13,new_str);
str.insert(0,new_str);
System.out.println("Now
the String is: "+str);
}
}
Output
F:\test>javac
StringBuffDemo4.java
F:\test>java
StringBuffDemo4
Initially
the String is: in need is a in deed
Now the
String is: FRIEND in need is a FRIEND in deed
F:\test>
Program Explanation
Using
the insert method, the string or numeric value or any character can be inserted
at the specified location in the given string. The program is self explanatory.
Ex. 4.11.5 Write a simple Java
program to check the palindrome of the given string.
Sol. :
Java Program [StringBuffDemo5.java]
public
class StringBuffDemo5{
public
static void main(String args[])
{
String
str1=new String("Java");
StringBuffer
str2=new StringBuffer(str1);
str2.reverse();
System.out.println("Initially
the String is: "+str1);
System.out.println("Reversed
String is: "+str2);
if(str1.equals(str2.toString()))
//checks if str1=str2
System.out.println("This
string is palindrome");
else
System.out.println("This
string is not palindrome");
}
}
Output
F:\test>javac
StringBuffDemo5.java
F:\test>java
StringBuffDemo5
Initially
the String is: Java
Reversed
String is: avaJ
This string
is not palindrome
Ex. 4.11.6 : Write a simple Java
program to change the string "impossible" to "possible".
Sol.:
Java Program [StringBuffDemo6.java]
public
class StringBuffDemo6{
public
static void main(String args[])
{
StringBuffer
str1 = new StringBuffer("Impossible");
System.out.print("The
word "+str1);
str1.delete(0,2);
System.out.println("
is changed to "+str1);
}
}
Output
F:\test>javac
StringBuffDemo6.java
F:\test>java
StringBuffDemo6
The
String Impossible is changed to possible
Program Explanation
In above program, we have used the method delete for deleting the some characters. The first argument denotes the starting character and the second character denotes the ending character.
Object Oriented Programming: Unit IV: I/O, Generics, String Handling : Tag: : Methods with purpose and Example Java Programs - StringBuffer Class
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation