String Buffer (Part 1)
Pre-requisites:- Strings
String Buffer
In Java , Strings are immutable i.e once declared cannot be changed. The StringBuffer class can create mutable strings. The only difference between String class and StringBuffer class is that StringBuffer creates mutable strings.
Important StringBuffer Methods:-
1. StringBuffer length( ) method
The length( ) method returns the length of the string.
public class
MyClass
{ public static void main(String[] args) {
// create a StringBuffer object 'sb'
StringBuffer
sb
= new StringBuffer("
Hello
"); System.out.println(
sb
.toString());
// outputs Hello
System.out.println(
sb
.length());
// outputs 5
} }
The .toString( ) displays StringBuffer in the form of a string.
2. StringBuffer append( string ) method
The append( ) method concatenates the string entered as argument in it.
public class
MyClass
{ public static void main(String[] args) {
//create a StringBuffer object 'sb'
StringBuffer
sb
=new StringBuffer("
Hello
"); System.out.println(
sb
.toString());
//outputs Hello
sb.append("
Java Monk
");
//adds string to sb
sb.append("
!
");
//adds string to sb
System.out.println(
sb
.toString());
//outputs Hello Java Monk !
} }
3. StringBuffer insert( int beginIndex, string ) method
The insert( ) method inserts specified string at user defined index.
public class
MyClass
{ public static void main(String[] args) {
//create a StringBuffer object 'sb'
StringBuffer
sb
=new StringBuffer("
Hello
"); System.out.println(
sb
.toString());
//outputs Hello
sb
.insert(
2
,"
!!!
");
//inserts '!!!' at index 2
System.out.println(
sb
.toString());
//outputs He!!!llo
} }
4. StringBuffer replace( int beginIndex, int endIndex, string ) method
The replace( ) method replaces the given string from the specified beginIndex and endIndex.
public class
MyClass
{ public static void main(String[] args) {
//create a StringBuffer object 'sb'
StringBuffer
sb
=new StringBuffer("
Hello
"); System.out.println(
sb
.toString());
//outputs Hello
sb.replace(
1
,
2
, "
Java Monk
");
//inserts 'Java Monk' at between Hello at specified index
System.out.println(
sb
.toString());
//outputs HJava Monkllo
} }
5. StringBuffer delete( int beginIndex, int endIndex ) method
The delete( ) method deletes the string character between specified beginning and ending indexes.
public class
MyClass
{ public static void main(String[] args) {
//create a StringBuffer object 'sb'
StringBuffer
sb
=new StringBuffer("
HelloWorld
"); System.out.println(
sb
.toString());
//outputs HelloWorld
sb
.delete(
3 , 7
);
//deletes characters between indexes 3 to 7
System.out.println(
sb
.toString());
//outputs Helrld
} }
6. StringBuffer substring( int beginIndex, int endIndex ) method
The substring( ) method returns the string from specified index.
public class
MyClass
{ public static void main(String[] args) {
// create a StringBuffer object 'sb'
StringBuffer
sb
= new StringBuffer("
Hello Java Monk
"); System.out.println(
sb
.toString());
// outputs Hello Java Monk
System.out.println(
sb
.substring(
2
,
10
));
// outputs llo Java
} }
7. StringBuffer charAt( index i ) method
The charAt( ) returns the character at specified index.
public class
MyClass
{ public static void main(String[] args) {
// create a StringBuffer object 'sb'
StringBuffer
sb
= new StringBuffer("
Hello Java Monk
"); System.out.println(
sb
.toString());
// outputs Hello Java Monk
System.out.println(
sb
.charAt(
6
));
// outputs J
} }
8. StringBuffer indexOf( string ) method
The indexOf( ) method returns the index of specified string character. If string not found , returns -1.
public class
MyClass
{ public static void main(String[] args) {
// create a StringBuffer object 'sb'
StringBuffer
sb
= new StringBuffer("
Hello Java Monk
"); System.out.println(
sb
.toString());
// outputs Hello Java Monk
System.out.println(
sb
.indexOf("
M
"));
// outputs 11
System.out.println(
sb
.indexOf("
s
"));
//outputs -1
} }
9. StringBuffer reverse( ) method
The reverse( ) method reverse characters position of entered string.
public
Myclass
{ public static void main(String[] args) {
//create a StringBuffer object 'sb'
StringBuffer
sb
=new StringBuffer("
Hello
"); System.out.println(
sb
.toString());
//outputs Hello
sb
.reverse();
//reverse string
System.out.println(
sb
.toString());
//outputs olleH
} }
10. StringBuffer capacity( ) method
The capacity( ) method , is used to return the current capacity.
public class
MyClass
{ public static void main(String[] args) {
// create a StringBuffer object 'sb'
StringBuffer
sb
= new StringBuffer(""); System.out.println(
sb
.toString());
// outputs blank line
System.out.println(
sb
.capacity());
// outputs default capacity i.e 16
StringBuffer
sb1
= new StringBuffer("
Hello there, It is Java Monk
"); System.out.println(
sb1
.capacity());
// outputs capacity 44
} }