Stringbuffer Stringbuffer (Int Size) Stringbuffer (String STR)
Stringbuffer Stringbuffer (Int Size) Stringbuffer (String STR)
Constructors
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
Egs
StringBuffer sb1=new
StringBuffer();
StringBuffer sb2=new
StringBuffer(25);
StringBuffer Sb3=new
StringBuffer(“Hai”);
Methods
public int length()
-Returns the current length of
StringBuffer
public void
setLength(int length)
Methods Contd…….
public char charAt(int index)
-Returns the char value in this
sequence at the specified index.
public void
setCharAt(int index, char ch)
-The character at the specified
index is set to ch.
Methods Contd…….
public void
getChars(int srcBegin,
int srcEnd, char[] dst,
int dstBegin)
public StringBuffer
deleteCharAt(int index)
-Removes the char at the specified
position in this sequence
Methods Contd…….
Eg:
StringBuffer s=new StringBuffer(“This
is a test.”);
s.delete(4,7);
S.o.p(“After delete: ”+s);
s.deleteCharAt ”+sb);
S.o.p(“Deleting Single char: ”+s);
//will display
// After delete: This a test
Methods Contd…….
public StringBuffer
replace(int start, int end, String
str)
-Replaces the characters in a substring
of this sequence with characters in the
specified String
public String
substring(int index)
-Returns portion of the String starting
from index. Another form
Eg:
StringBuffer sb=new StringBuffer(“This
is a test”);
sb.replace(5,7,”was”);
S.o.p(“After replace:”+sb);
String S1=substring(5);
String S2=substring(5,8);
S.o.p(“S1:”+S1);
S.o.p(“S2:”+S2);
O/P
After replace:This was a test
S1:was a test
S2:was