Latest web development tutorials

Java StringBuffer and StringBuilder class

When the string to be modified when the need to use StringBuffer and StringBuilder classes.

And the String class is different, StringBuffer and StringBuilder classes of objects can be modified many times, and does not create new unused objects.

StringBuilder class is made in Java 5, the biggest difference between it and the StringBuffer StringBuilder is not thread-safe method (can not synchronize access).

Because compared to StringBuffer StringBuilder has the speed advantage, so in most cases recommend using the StringBuilder class. However, in applications that require thread safe, you must use the StringBuffer class.

Examples

public class Test{

    public static void main(String args[]){
       StringBuffer sBuffer = new StringBuffer(" test");
       sBuffer.append(" String Buffer");
       System.out.println(sBuffer);  
   }
}

The above examples compiled results are as follows:

test String Buffer

StringBuffer Methods

Here are the main method of StringBuffer class supports:

No. Method Description
1 public StringBuffer append (String s)
The specified string to this character sequence.
2 public StringBuffer reverse ()
This sequence of characters replaced by the reverse form.
3 public delete (int start, int end)
Remove substring of this sequence of characters.
4 public insert (int offset, int i)
The int string representation of the argument into this sequence.
5 replace (int start, int end, String str)
Given String of characters to replace the substring of this sequence of characters.

The following list of methods and method of the String class like this:

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