Вы находитесь на странице: 1из 18

StringBuffer Class In Java

1
StringBuffer class in Java
• Peer class of String
• String represents fixed length and immutable
character sequence
• StringBuffer allows growable and writable character
sequence
• Characters in StringBuffer can be
inserted/appended/added/deleted any where and the
size of the StringBuffer will automatically
grow/shrink to make room

2
StringBuffer Constructors
• StringBuffer()
<< Reserves room for 16 characters >>
• StringBuffer(int size)
<< Explicitly sets the size of buffer >>
• StringBuffer(String str)
<< Sets the initial content of the string
Buffer and allocates room for 16 more
characters>>

<< When no specific length is specified ,


StringBuffer allocates room for 16 more
characters>> 3
int length()
int capacity()
• int length() returns the current length of string buffer
• int capacity() returns the allocated capacity
• Example :

StringBuffer strbuf = new StringBuffer();


System.out.println(strbuf.length()); 0
System.out.println(strbuf.capacity()); 16

StringBuffer strbuf1 = new StringBuffer("Object");


System.out.println(strbuf1.length()); 6
System.out.println(strbuf1.capacity()); 22

4
void ensureCapacity(int capacity)
• Useful if you know in advance the size of buffer
• Used to preallocate room for a certain number of characters
after StringBuffer object has been created.
• <<capacity>> specifies the size of buffer

StringBuffer strbuf = new StringBuffer();


System.out.println(strbuf.length()); 0
System.out.println(strbuf.capacity()); 16
strbuf.ensureCapacity(10);
System.out.println(strbuf.capacity()); 16

StringBuffer strbuf1 = new StringBuffer("Object");


System.out.println(strbuf1.length()); 6
System.out.println(strbuf1.capacity()); 22
strbuf1.ensureCapacity(20);
System.out.println(strbuf1.capacity()); 22 5
StringBuffer strbuf = new StringBuffer();
System.out.println(strbuf.length()); 0
System.out.println(strbuf.capacity()); 16
strbuf.ensureCapacity(20);
System.out.println(strbuf.capacity()); 34

StringBuffer strbuf1 = new StringBuffer("Object");


System.out.println(strbuf1.length()); 6
System.out.println(strbuf1.capacity()); 22
strbuf1.ensureCapacity(30);
46
System.out.println(strbuf1.capacity());

What will happen for the following statements

strbuf.ensureCapacity(-20); NO EFFECT ON
strbuf1.ensureCapacity(-30); CAPACITY
6
StringBuffer s1 = new StringBuffer("Java");
System.out.println(s1.length()); 4
System.out.println(s1.capacity()); 20
s1.ensureCapacity(50);
System.out.println(s1.length()); 4
System.out.println(s1.capacity()); 50

7
void setLength(int len)

• Sets the length of the buffer within StringBuffer


object
• << len >> specifies the length of the buffer
• << len >> should be non negative
• If <<len>> is less than current length returned by
length() then extra characters will be lost

8
StringBuffer strbuf = new StringBuffer();
System.out.println(strbuf.length()); 0
System.out.println(strbuf.capacity());16
strbuf.setLength(20);
System.out.println(strbuf.length()); 20
System.out.println(strbuf.capacity()); 34

StringBuffer strbuf1 = new StringBuffer("Object oriented");


System.out.println(strbuf1.length()); 15
System.out.println(strbuf1.capacity()); 31
strbuf1.setLength(20);
System.out.println(strbuf1.length()); 20
System.out.println(strbuf1.capacity()); 31

StringBuffer strbuf2 = new StringBuffer("Object oriented");


System.out.println(strbuf2.length()); 15
System.out.println(strbuf2.capacity()); 31
strbuf2.setLength(40);
System.out.println(strbuf2.length()); 40
System.out.println(strbuf2.capacity()); 64 9
char charAt(int where)
void setCharAt(int where, char ch)

• charAt() method is same as String class i.e returns a char from


index where
• setCharAt() method sets the character ch at the where index.
• <<where>> should be >= 0 and should not specify a position
beyond the end of the buffer

StringBuffer strbuf = new StringBuffer("Object oriented");


System.out.println(strbuf); Object oriented
strbuf.setCharAt(10,'X');
System.out.println(strbuf);
Object oriXnted

10
Appending [Adding at the End] String Buffer

• append() method can be used for adding at the end of


StringBuffer.
• Append() is overloaded with following foms:
1. StringBuffer append(String str)
2. StringBuffer append(int num)
3. StringBuffer append(Object obj)
• First Method adds String str in the end
• Second converts num into string and then adds in the end
• Third method calls toString() on obj and then String form of
obj will be inserted in the end. [ In this case obj must supply
a suitable toString() method otherwise it will be taken from
Object]

11
Append Example
class Circle
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
} class CircleTest
{
public static void main(String args[])
{
StringBuffer strbuf = new StringBuffer("Object");

strbuf.append(" oriented");
System.out.println(strbuf);
Object oriented
strbuf.append(6.1);
System.out.println(strbuf); Object oriented6.1
12
strbuf.append(6.1);
System.out.println(strbuf); Object oriented6.16.1

Circle c1 = new Circle(10.56);

strbuf.append(c1);
System.out.println(strbuf);
} Object oriented6.16.1Circle@82ba41
}

Hashcode of Circle

13
class Circle
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
public String toString()
{
return "Circle with Radius:"+radius;
}
} OUTPUT
class CircleTest
JavaCircle with Radius:10.56
{
public static void main(String args[])
{
StringBuffer strbuf = new StringBuffer("Java");
Circle c1 = new Circle(10.56);
strbuf.append(c1);
System.out.println(strbuf);
} 14
}
Inserting charcters
• Insert() method can be used for inserting
characters
• Insert() method is also overloaded
1. StringBuffer insert(int index , String str);
2. StringBuffer insert(int index , char ch);
3. StringBuffer insert(int index, Object obj);
• << index >> must be within permitted range and
should be positive

15
StringBuffer strbuf = new StringBuffer("Java");
strbuf.insert(3," "); Jav a
System.out.println(strbuf);

StringBuffer strbuf = new StringBuffer("Java");


strbuf.insert(4," "); Java
System.out.println(strbuf);
System.out.println(strbuf.length()); 5

StringBuffer strbuf = new StringBuffer("Java");


strbuf.insert(2,“ Programming ");
System.out.println(strbuf);
JaProgrammingva
System.out.println(strbuf.length()); 15
StringBuffer strbuf = new StringBuffer("Java");
strbuf.insert(2,new circle(10));
System.out.println(strbuf); ?
16
System.out.println(strbuf.length());
Reverse the String Buffer
• Use reverse() method
• Syntax:
StringBuffer reverse();
• Examples :
StringBuffer s1 = new StringBuffer(“Object”);
System.out.println(s1.reverse());

17
Deleting Characters
Deleting a single charcater
• To delete a single character use deleteCharAt() method
• Syntax deleteCharAt() :
StringBuffer deleteCharAt(int loc)
<< Deletes a character from index indicated by loc>>
• <<loc>> should be positive and within permitted range.

Deleting a Range of charcaters


• To delete characters in range use delete() method
• Syntax delete() :
StringBuffer delete(int startIndex , int endIndex)
<< Deletes characters from startIndex to endIndex -1 >>
• startIndex,endIndex should be positive and within permitted
range and endIndex > startIndex 18

Вам также может понравиться