Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java StringBuffer and StringBuilder Classes

Java StringBuffer and StringBuilder Classes

When modifying strings, you need to use the StringBuffer and StringBuilder classes.

Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without producing new unused objects.

When using the StringBuffer class, each operation acts on the StringBuffer object itself rather than generating a new object, so if you need to modify strings, StringBuffer is recommended.

The StringBuilder class was introduced in Java 5. Its biggest difference from StringBuffer is that StringBuilder's methods are not thread-safe (not synchronized).

Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended in most cases.

Example

public

class

RunoobTest

{

public

static

void

main

(

String

[

]

args

)

{

StringBuilder

sb

=

new

StringBuilder

(

10

)

;

sb

.

append

(

"

Runoob..

"

)

;

System

.

out

.

println

(

sb

)

;

sb

.

append

(

"

!

"

)

;

System

.

out

.

println

(

sb

)

;

sb

.

insert

(

8

,

"

Java

"

)

;

System

.

out

.

println

(

sb

)

;

sb

.

delete

(

5

,

8

)

;

System

.

out

.

println

(

sb

)

;

}

}

The compilation and execution results of the above example are as follows:


Runoob..
Runoob..!
Runoob..Java!
RunooJava!

However, when the application requires thread safety, the StringBuffer class must be used.

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

StringBuffer

sBuffer

=

new

StringBuffer

(

"

Runoob Tutorial official site:

"

)

;

sBuffer

.

append

(

"

www

"

)

;

sBuffer

.

append

(

"

.runoob

"

)

;

sBuffer

.

append

(

"

.com

"

)

;

System

.

out

.

println

(

sBuffer

)

;

}

}

The compilation and execution results of the above example are as follows:


Runoob Tutorial官网:www.runoob.com

StringBuffer Methods

The following are the main methods supported by the StringBuffer class:

No. Method Description
1 public StringBuffer append(String s)
Appends the specified string to this character sequence.
2 public StringBuffer reverse()
 Replaces this character sequence with its reversed form.
3 public delete(int start, int end)
Removes the characters in a substring of this sequence.
4 public insert(int offset, int i)
data to the child component via int Inserts the string representation of the argument into this sequence.
5 insert(int offset, String str)
data to the child component via str Inserts the string of the argument into this sequence.
6 replace(int start, int end, String str)
Using the given String replaces the characters in a substring of this sequence with characters from

The following list shows other commonly used methods of the StringBuffer class:

No. Method Description
1 int capacity()
Returns the current capacity.
2 char charAt(int index)
Returns the character at the specified index in this sequence char values.
3 void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum.
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this sequence into the destination character array dst.
5 int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
6 int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
7 int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.
8 int lastIndexOf(String str, int fromIndex)
Returns the position of the last occurrence of the substring in the String object.
9 int length()
 Returns the length (number of characters).
10 void setCharAt(int index, char ch)
Sets the character at the given index to ch.
11 void setLength(int newLength)
Sets the length of the character sequence.
12 CharSequence subSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.
13 String substring(int start)
Returns a new String, which contains the subsequence of characters currently contained in this character sequence.
14 String substring(int start, int end)
Returns a new String, which contains the subsequence of characters currently contained in this sequence.
15 String toString()
Returns the string representation of the data in this sequence.

More information: