0% found this document useful (0 votes)
5 views

StringBuffer Class

StringBuffer Class
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

StringBuffer Class

StringBuffer Class
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java StringBuffer Class

S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
Java StringBuffer class is used to create mutable
(modifiable) String objects.
The StringBuffer class in Java is the same as
String class except it is mutable i.e. it can be
changed.
Important methods of StringBuffer class
StringBuffer Class append() Method
The append() method concatenates the given argument
with this String.
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Output:
Hello Java
StringBuffer insert() Method
• The insert() method inserts the given String with this string
at the given position.
StringBufferExample2.java
class StringBufferExample2{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
Output:
HJavaello
StringBuffer replace() Method
• The replace() method replaces the given String from the
specified beginIndex and endIndex.
StringBufferExample3.java
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
Output:
HJavalo
StringBuffer delete() Method
• The delete() method of the StringBuffer class deletes
the String from the specified beginIndex to endIndex.
class StringBufferExample4{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
Output:
Hlo

You might also like