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

StringBuffer Class in Java

The StringBuffer class provides mutable string objects. It has constructors to create empty or initialized StringBuffer objects. Methods like append(), insert(), replace(), delete(), and deleteCharAt() allow modifying the StringBuffer content after creation. The capacity of the StringBuffer is automatically expanded as needed when characters are added.

Uploaded by

Neema
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

StringBuffer Class in Java

The StringBuffer class provides mutable string objects. It has constructors to create empty or initialized StringBuffer objects. Methods like append(), insert(), replace(), delete(), and deleteCharAt() allow modifying the StringBuffer content after creation. The capacity of the StringBuffer is automatically expanded as needed when characters are added.

Uploaded by

Neema
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 6

StringBuffer classes

Overview

 StringBuffer class

 Some StringBuffer methods

1
StringBuffer class
 A String object is not modifiable once created (i.e.,
it is immutable).

 Modifiable (i.e., mutable) strings are supplied by


the StringBuffer class
 It is defined in the package java.lang

 A String buffer object can be created using any of


the following constructors:

 Note: If the capacity of a StringBuffer object is


exceeded, the buffer is automatically expanded to
accommodate the additional characters.

2
StringBuffer Constructors
StringBuffer( )
StringBuffer st = new StringBuffer();

Creates a StringBuffer object containing no


characters in it with an initial capacity of 16
characters

StringBuffer(int StringBuffer st2 = new StringBuffer(10);


size) throws
Creates a StringBuffer object containing no
NegativeArraySiz characters in it with an initial capacity of size
eException characters. Throws an exception if size < 0

StringBuffer StringBuffer st3 = new StringBuffer


(String str) (“Hello”);

Creates a StringBuffer object containing str with an


initial capacity of (str.length( ) + 16) characters

3
Some StringBuffer methods

int length( )
Returns the number of characters in the buffer

StringBuffer sb=new StringBuffer(“Hello”);


System.out.println(sb.length());
5
int capacity( )
Returns the capacity of the buffer
System.out.println(sb.capacity());
21
char charAt(i)
Returns the character at position I
System.out.println(sb.charAt(1));
E
void setCharAt(i, ch)
Replaces char at i with ch
Sb.setCharAt(1,’E’);
System.out.println(sb);
Hello

4
Some StringBuffer methods
StringBuffer append(ob)

Appends the string form of the object ob or a primitive value to the


buffer.

StringBuffer sb=new StringBuffer(“Hello”);


String s=” World”;
Sb=sb.append(s);
System.out.println(sb);
Hello World
StringBuffer insert(i, ob)

Inserts the string form of the object ob (or a primitive value) at i

StringBuffer sb=new StringBuffer(“Hello world”);


Sb.insert(6,”Java”);
System.out.println(sb);

Hello Java World

5
Some StringBuffer methods
StringBuffer replace(s, f, str)

Replaces the substring from s to f – 1 with str

StringBuffer sb=new StringBuffer(“Hello world”);


Sb.replace(0,5,”Hi”);
System.out.println(sb);
Hi world

StringBuffer delete(s, f)
Deletes characters from s to f - 1
StringBuffer sb=new StringBuffer(“Hello world”);
Sb.delete(0,2);
System.out.println(sb);
llo
StringBuffer deleteCharAt(i)
Deletes the character at i
StringBuffer sb=new StringBuffer(“Hello world”);
Sb.delete(2);
System.out.println(sb);
Helo
6

You might also like