0% found this document useful (0 votes)
59 views

Stringbuffer Stringbuffer (Int Size) Stringbuffer (String STR)

This document describes the StringBuffer class in Java. It lists the constructors StringBuffer(), StringBuffer(int size), and StringBuffer(String str). It then describes several methods of the StringBuffer class including length(), capacity(), ensureCapacity(), setLength(), charAt(), setCharAt(), getChars(), append(), insert(), reverse(), delete(), deleteCharAt(), replace(), and substring(). Examples are provided to demonstrate the usage of these methods.

Uploaded by

VinayKumarSingh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT or read online on Scribd
0% found this document useful (0 votes)
59 views

Stringbuffer Stringbuffer (Int Size) Stringbuffer (String STR)

This document describes the StringBuffer class in Java. It lists the constructors StringBuffer(), StringBuffer(int size), and StringBuffer(String str). It then describes several methods of the StringBuffer class including length(), capacity(), ensureCapacity(), setLength(), charAt(), setCharAt(), getChars(), append(), insert(), reverse(), delete(), deleteCharAt(), replace(), and substring(). Examples are provided to demonstrate the usage of these methods.

Uploaded by

VinayKumarSingh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT or read online on Scribd
You are on page 1/ 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