You are on page 1of 14

StringBuffer

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 int capacity()


-Returns total allocated capacity
Methods Contd…….
public void
ensureCapacity(int capacity)
-Ensures that the capacity is at
least equal to the
specified minimum .

 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)

-Characters are copied from this


sequence into the destination
character array dst
Methods Contd…….
public StringBuffer
append(Type var)
-Concatenates the string
representation of any other
type of data
-Calls String.valueOf()
Eg:
int a=90;
StringBuffer sb=new
Methods Contd…….
public StringBuffer
insert(int index, Type var)
-Inserts one string to another
-can insert string representation of
any other type of data.
Eg: StringBuffer sb=new StringBuffer(“I
JAVA”);
sb.insert(2,”LIKE”);
S.o.p(sb);//will produce
Methods Contd…….
 public StringBuffer reverse()
-Causes this character sequence to
be replaced by the reverse of the
sequence.
 Eg: StringBuffer sb=new
StringBuffer(“HELLO”);
sb.reverse();
S.o.p(sb);//will print
//OLLEH
Methods Contd…….
 public StringBuffer
delete(int start, int end)
-Removes a sequence of characters

 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

You might also like