60 likes | 207 Vues
String Buffer Class. An object of String buffer represents mutable sequence of character. Constructor:- Public StringBuffer (); Public StringBuffer ( int capacity); Public StringBuffer (String s); E.g. stringBuffer s=new StringBuffer (); StringBuffer t=new StringBuffer (10);
E N D
String Buffer Class An object of String buffer represents mutable sequence of character. Constructor:- Public StringBuffer(); Public StringBuffer(int capacity); Public StringBuffer(String s); E.g. stringBuffer s=new StringBuffer(); StringBuffer t=new StringBuffer(10); StringBuffer u=new StringBuffer(“ABC”);
Each StringBuffer Object has a initial capacity that represents maximum number of characters that can be stored in the stringBuffer object without changing its size. • Capacity of a stringBuffer object can be found with the help of capacity method . • Public int capacity(); • S.O.P(s.capacity()); 16 • S.O.P(t.capacity()); 10 • S.O.P(u.capacity()); 19 • Default capacity of a string Buffer object is 16.
Methods of string Class :- • Append():- is used at the contents of a string at the end of invoking string buffer Object. • Public StringBuffer append(String s); • StringBuffer s= new StringBuffer(“ABC”); • StringBuffer t=s.append(“DEF”); • S.o.P(s);//ABCDEF • S.O.p(t);//ABCDEF • S.o.p(s==t); True NOTE:returns of S.B. class that changes the contents of S.B. Object returns the reference of invoking object so that method cal can be chain.
Ex:StringBuffer s=new StringBuffer(“ABC”); • S.append(“ABC”).insert(3,”PQR”); • s.insert(3,”PQR”); • insert():- • It is used to insert a string at the specified position in a stringBuffer object. • Public StringBuffer insert(int index, String s); • replace():-it is used contents at the specified index from the specified string . • Public StringBuffer replace(int start index, int end index; string s);
Stringbuffer a=new StringBuffer(“abc”); • a.append(“pqr”).insert(2,”xyz”);//abcpqr&abxyzcpqr • replace(0,1,”mno”); //mnobxyzcpqr • Reverse():- • Public stringBuffer reverse(); • toString():- public String toString();