Java String Buffer和StringBuilder类
When the string changes, the need and the StringBuilder class String Buffer
- It can be modified several times, but do not generate new unused objects
- StringBuilder class is not thread safe.
-
Speed advantage, in the case of safety requirements must be StringBuffer class
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer("everyone:"); sBuffer.append("say: "); sBuffer.append("Hello,"); sBuffer.append("world"); System.out.println(sBuffer); } }
-
StringBuffer methods
StringBuffer methods
Numbering | method |
---|---|
1 | public StringBuffer appen (String s) specified string to this character sequence |
2 | public StringBuffer reverse () reverse text |
3 | public StringBuffer delete (int start, int end) delete part of a string |
4 | public StringBuffer insert (int offset, int i) will be inserted into this sequence int string representation of the parameter |
5 | replace (int start, int end, String str) replace the character string with the specified string |
No. | method |
---|---|
1 | int capacity () Returns the current capacity |
2 | char charAt (int index) Returns the specified sequence char value at index |
3 | void ensureCapacity (int minimumCapacity) ensure at least equal to the specified minimum capacity |
4 | void ensureCapacity (int secBegin, int srcEnd , char [] dst, int dstBegin) will be copied to the target sequence string from character array dst |
5 | int indexOf (String str) Returns the specified substring index of the first occurrence of the string |
6 | int indexOf (String str, int fromIndex ) starting from the specified index, substring Returns the index of the first occurrence of the string |
7 | int lastIndexOf (String str) Returns the rightmost occurrence of the specified string index within this string of |
8 | nt lastIndexOf (String str, int fromIndex ) Returns a String object position of the last occurrence of |
9 | int length () Returns the length (number of characters) |
10 | void setCharAt (int index, char ch ) character set for a given index ch |
11 | void setLength (int newLength) provided 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 subsequence |
15 | String toString () Returns the string representation of the data sequence |