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 creating new unused objects.
The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that StringBuilder The method is not thread-safe (thread safety means that when multi-threads access, a locking mechanism is used. When a thread accesses certain data of this class, it is protected and other threads cannot access it until the thread finishes reading. Other threads It can be used. There will be no data inconsistency or data pollution. Thread unsafe means that it does not provide data access protection, and it is possible that multiple threads change the data successively and the resulting data is dirty data).
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.
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
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 sequence of characters 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 argument into this sequence. |
5 | replace(int start, int end, String str) Replaces the characters in a substring of this sequence with the characters in the given String . |
The methods in the following list are similar to those 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) 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 to the target character array dst . |
5 | int indexOf(String str) Returns the index of the first occurrence of the specified substring in the string. |
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 in this string of the rightmost occurrence of the specified substring. |
8 | int lastIndexOf(String str, int fromIndex) Returns the index in this string of the last occurrence of the specified substring. |
9 | int length() returns the length in 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 containing the character subsequence 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 the string representation of the data in this sequence. |