String 、StringBuffer 和StringBuilder类

 

String class is immutable, so once a String object is created, its value can not be changed.

When the modified time string, and the need to use StringBuilder class StringBuffer

And String are different, and StringBuffer StringBuilder class objects can be repeatedly modified, and no new unused objects.

StringBuilder StringBuffer have relative speed advantage, so most use StringBuilder class, but in the case of application requires thread-safe, you must use the StringBuffer class.

public class Test{
    public static void main(String args[]){
        StringBuffer sBuffer = new StringBuffer("今天");
        sBuffer.append("又");
        sBuffer.append("下雨");
        sBuffer.append("了");
        System.out.println(sBuffer);
    }
}

operation result

It's raining again today
The main method of StringBuffer class support
No. method description
1 public StringBuffer append(String s) The specified string to this character sequence
2 public StringBuffer reverse() This string is replaced by the reverse of the
3 public delete(int start,int end) Remove this sequence of characters in a string
4 public insert(int offset,int i) The int string representation of the parameter into this sequence
5 replace(int start,int end,String str) Given String of characters replace the sequence of characters in the string
 

And a method similar to the method of the String class

No. method description
1 int capacity() Returns the current capacity
2  char charAt(int undex) Returns value char sequence index 
3  void ensureCapacity(int minimunCapacity)  Ensure that at least the specified minimum capacity is equal to
4  void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) Will be copied to the target sequence of characters from a character array dsc 
5  int indexOf(String str) Returns the specified string in the index of the first occurrence of the character of 
6  int lastIndexOf(String str) Returns the rightmost occurrence of the specified string index within this string of 
7  int length() Returns the length (number of characters) 
8  void setCharAt(int index,char ch) The character at the specified index is set to ch 
9  void setLength(int newLength) Set character sequence length 
 10  CharSequence subSequence(int start,int end) Returns a new character sequence, the character sequence is a subsequence of this sequence 
11   String substring(int start) Returns a new string, it contains character sequences before but this character sequence contained 
12   String toString() Returns the string representation of the data sequence 
     
     
     

Guess you like

Origin www.cnblogs.com/jaci/p/11421575.html