Java StringBuffer
When modifying strings, you need to use the StringBuffer and StringBuilder classes.
Different from the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects.
The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously).
Since StringBuilder has a speed advantage compared to StringBuffer, it is recommended to use the StringBuilder class in most cases. However, when the application requires thread safety, the StringBuffer class must be used.
Example
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(" test"); sBuffer.append(" String Buffer"); System.out.println(sBuffer); } }
The compilation and running results of the above example are as follows:
test String Buffer
StringBuffer method
The following are the main methods supported by the StringBuffer class:
Serial number | 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 characters from a substring of this sequence. |
4 |
public insert(int offset, int i) Inserts the string representation of the int parameter into this sequence. |
5 |
replace(int start, int end, String str) Replaces characters in a substring of this sequence with characters in the given String . |
The methods in the following list are similar to the methods of the String class:
Serial number | Method description |
---|---|
1 |
int capacity() Returns the current capacity. |
2 |
char charAt(int index) Returns the char value at the specified index in this sequence. |
3 |
void ensureCapacity(int minimumCapacity) Make sure 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 to the destination character array dst . |
5 |
int indexOf(String str) Returns the index within the string of the first occurrence of the specified substring. |
6 |
int indexOf(String str, int fromIndex) Starting at the specified index, returns the index of the first occurrence of the specified substring in the string. |
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 index within this string of the last occurrence of the specified substring. |
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) Set 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 containing the subsequence of characters currently contained by this character sequence. |
14 |
String substring(int start, int end) Returns a new String containing the subsequence of characters currently contained by this sequence. |
15 |
String toString() Returns a string representation of the data in this sequence. |