StringBuffer class 3:
7)
append(): This method is used to append or add the character
or the string at the end of the string . This method has two forms:
append(int ch)
append(String s)
E.g.
StringBuffer sb=new StringBuffer("Quantum");
StringBuffer s;
s=sb.append("adcom") ;
s= s.append(" is") ;
System.out.println(s);
Output:
Quantumadcom is
8)
insert(): This method is used to insert the character or the
string at the particular position in the string . This method has the two
different forms:
insert(int index,int ch);
insert(int index,String s);
StringBuffer sb=new
StringBuffer("computer");
sb.insert(2,'a');
System.out.println(sb);
sb.insert(2,"today");
System.out.println(sb);
Output:
coamputer
cotodayamputer
9)
reverse(): This method is used to reverse the string .
StringBuffer sb=new
StringBuffer("comp");
System.out.println(sb);
sb.reverse();
System.out.println(sb);
Output:
comp
pmoc
10)
delete(): This method is used to delete the specific group of
characters from the string .
The general form
is:
delete(int sindex,int eindex)
The characters are actually deleted
till the position eindex-1
E.g.
StringBuffer sb=new
StringBuffer("computer");
System.out.println(sb);
sb.delete(3,6);
System.out.println(sb);
Output:
computer
comer
11)
deleteCharAt(): This method is used to delete the
character at the particular index position .
The general form
is:
deleteCharAt(int index)
Where:
index is the character position.
E.g.
StringBuffer sb=new
StringBuffer("comp");
System.out.println(sb);
sb.deleteCharAt(2);
System.out.println(sb);
Output:
comp
cop
Comments
Post a Comment