Page 358 - Computer science 868 Class 12
P. 358
Syntax:
StringBuffer object1.insert(index_to_insert,StringBuffer object2);
Example:
StringBuffer object1= new StringBuffer("Class 12 Science");
String str = "Computer ";
System.out.println(object1.insert(8,str));
Output : Class 12 Computer Science
c. delete(): The delete() method is used to remove a sequence of characters from the StringBuffer. It removes all the
characters from the start index till end index-1.
Syntax:
StringBuffer object1.delete(start_index,end_index-1);
Example:
StringBuffer object1= new StringBuffer("Class 12 Computer Science");
System.out.println(object1.delete(6,9));
Output : Class Computer Science
d. reverse(): The reverse() method is used to reverse the order of the characters in the StringBuffer.
Syntax:
StringBuffer object1.reverse();
Example:
StringBuffer object1= new StringBuffer("Science");
System.out.println(object1.reverse());
Output : ecneicS
e. replace(): The replace() method replaces the given string from the specified startIndex and endIndex-1.
Syntax:
StringBuffer object1.replace(Start_idex,end_index,word_to_insert);
Example:
StringBuffer object1 = new StringBuffer("Class 12 Computer Science");
System.out.println(object1.replace(6, 9, "Twelve "));
Output: Class Twelve Computer Science
f. capacity():The capacity() method of the StringBuffer class returns the current capacity of the StringBuffer. The
capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
Syntax: int object1.capacity();
Example:
StringBuffer object1 = new StringBuffer("Class 12");
System.out.println(object1.capacity());
Output:8
What is the difference between String class and StringBuffer class?
String Class StringBuffer Class
A string object has fixed length. A StringBuffer object can change its length depending
on requirement.
It is slower during concatenation. It is faster during concatenation.
Used in simple programming. Used in complex String Programming.
356356 Touchpad Computer Science-XII

