Methods | Description |
---|---|
public StringBuffer append(String s) | Append the specified string with the given string. |
public insert(int offset, String s) | Insert the specified string with given string at the specified position. |
public StringBuffer reverse() | Returns the reverse of string. |
public delete(int start, int end) | Delete substring from specified start index to end index. |
replace(int start, int end, String str) | Replace the characters in a substring of current sequence with the characters in the specified new string. |
int capacity() | Returns the current capacity of the string. |
void ensureCapacity(int minimumCapacity) | Ensure the capacity at least equal to given minimum capacity. |
int length() | Returns the length of StringBuffer object. This method is used to count the characters in specified string. |
String toString() | Returns a string representing the data in this sequence. |
String substring(int start) | Returns a new string that contains the sub sequence of the characters currently contained in this string. |
public class StringBufferDemo
{
public static void main(String args[])
{
// append
StringBuffer sb1 = new StringBuffer("Welcome");
StringBuffer sb2 = sb1.append(" Java");
System.out.println("sb2 = "+sb2);
System.out.println("Length of sb2 = "+sb2.length());
//insert
StringBuffer sb3 = new StringBuffer("10Aug1992");
sb3.insert(2,"-");
sb3.insert(6,"-");
System.out.println("sb3 = "+sb3);
}
}
public class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer sb1 = new StringBuffer("Java Tutorials");
StringBuffer sb2 = new StringBuffer("Java");
StringBuffer sb3 = new StringBuffer("ABC");
//delete
sb1.delete(0, 5);
System.out.println("After delete sb1= "+sb1);
//reverse
sb2.reverse();
System.out.println("Reverse of sb2= "+sb2);
//charAt()
for (int i = 0; i <= sb3.length()-1; i++)
{
System.out.println("Character at inedx "+i+" is: "+sb3.charAt(i));
}
}
}