|
The StringBuffer class has many methods for string manipulation. Following are some of the most commonly used methods which also important from the exam perspective.
The method modifies the StringBuffer object on which it is invoked. The contents of the string passed as an argument are appended to it. The signature of this method is:
public synchronized StringBuffer append(String s)
As we have seen earlier, this method will modify the contents of the StringBuffer object that invoked the method. This method is overloaded to take different types of arguments such as boolean, char, int, long, double, float etc. You can invoke the method that takes string argument as:
StringBuffer sb = new StringBuffer("set ");
sb.append("point");
System.out.println( sb ); // output is "set point"
You can invoke the append() method that takes char array and int value as argument as:
char[] charArray = new char[] {'O','n','l','y',' '};
StringBuffer sbuf = new StringBuffer();
sbuf.append(charArray);
sbuf.append(1);
System.out.println(sbuf); // prints "Only 1"
|
|
When string Concatenation is performed with operator +,
implicitly Java uses a StringBuffer object and its append() method. You might
recall that if any one of the expressions involving + operator is of type String
then the string concatenation takes place. Java implicitly creates a StringBuffer
object and appends the expressions as strings. |
The method modifies the StringBuffer object on which it is invoked. The argument is inserted in the StringBuffer object at a specified location. This method is overloaded to insert various types of argument. The method signature of a method that inserts another string is as:
public synchronized StringBuffer insert(int offset, String str)
The string passed as the second argument is inserted into the original StringBuffer object starting at the offset location represented by the first argument. For example, you can insert a string as:
StringBuffer sbuf = new StringBuffer("Good!");
sbuf.insert(4, " job");
System.out.println(sbuf); // "Good job!"
Note that the offset is zero-based. It means when the offset is 4, the insertion will take place at the 4th index which is same as fifth position from the left.
This method also modifies the StringBuffer object on which it is invoked. As its name suggests, it reverses the entire content of the StringBuffer object. The signature of this method is as:
public synchronized StringBuffer reverse()
When this method is invoked on a StringBuffer object, the first character takes the last position, the second one takes the second to last position until the last character of the content takes the very first position. For example,
StringBuffer sbuf = new StringBuffer("mirror");
sbuf.reverse();
System.out.println(sbuf); // prints "rorrim"
Note that the original StringBuffer object is changed with all its content reversed.
This method creates a string object using the contents of the StringBuffer object and returns it. The signature of this method is as:
public String toString()
You can get a string from the StringBuffer object by invoking this method as:
StringBuffer sbuf = new StringBuffer("This is a test");
String str = sbuf.toString();
System.out.println(str); // prints " This is a test"