method of string buffer 2.
4) charAt(int index):
This method is used to return the character present at the particular index position in the string
E.g.
StringBuffer sb=new StringBuffer("computer");
System.out.println(sb.charAt(3));
Output :
p
5) setCharAt(int index , int ch)
The method is used to set the character at a particular index position .
E.g.
StringBuffer sb=new StringBuffer("hello");
System.out.println(sb);
sb.setCharAt(1,'a');
System.out.println(sb);
Output:
hello
hallo
6) getChars():
This method is used to extract the specific group of characters from the string and assign it into the character array. The general form is:
getChars(int sindex,int eindex,char t[],int position)
Where:
sindex is the starting index position .
eindex is the ending index position.Actually the string is extracted upto the position eindex-1.
t is the target character array.
position is used to specify the index position from where the insertion will start in the target array
char t[]=new char[50];
StringBuffer sb=StringBuffer("computer");
sb.getChars(3,6,t,0);
for(int i=0;i<sb.length();i++)
System.out.print(t[i]);
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
Comments
Post a Comment