Java StringBuffer class
StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.
Syntax:
StringBuffer s = new StringBuffer(“text");
Important Constructors of StringBuffer class:-
Constructor |
Description |
StringBuffer() |
creates an empty string buffer with the initial capacity of 16. |
StringBuffer(String str) |
creates a string buffer with the specified string. |
StringBuffer(int capacity) |
creates an empty string buffer with the specified capacity as length. |
|
|
|
|||
public synchronized StringBuffer |
append(String s) |
is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc. |
|||
public synchronized StringBuffer |
insert(int offset, String s) |
is used to insert the specified string with this string at the specified position. The insert() method is overloaded like 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) |
is used to replace the string from specified startIndex and endIndex. |
|||
public synchronized StringBuffer |
delete(int startIndex, int endIndex) |
is used to delete the string from specified startIndex and endIndex. |
|||
public synchronized StringBuffer |
reverse() |
is used to reverse the string. |
|||
public int |
capacity() |
is used to return the current capacity. |
|||
public void |
ensureCapacity(int minimumCapacity) |
is used to ensure the capacity at least equal to the given minimum. |
|||
public char |
charAt(int index) |
is used to return the character at the specified position. |
|||
public int |
length() |
is used to return the length of the string i.e. total number of characters. |
|||
public String |
substring(int beginIndex) |
is used to return the substring from the specified beginIndex. |
|||
public String |
substring(int beginIndex, int endIndex) |
is used to return the substring from the specified beginIndex and endIndex. |
What is mutable string:-
A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.
1) StringBuffer append() method
The append() method concatenates the given argument with this string.
class StringBufferExamplepublic static void main(String args[])
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");
System.out.println(sb);
}
}
2) StringBuffer insert() method
The insert() method inserts the given string with this string at the given position.
public static void main(String args[])
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");
System.out.println(sb);
}
}
3) StringBuffer replace() method
class StringBufferExample3public static void main(String args[])
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);
}
}
4) StringBuffer delete() method
class StringBufferExample4public static void main(String args[])
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);
}
}
Comments
Post a Comment