StringBuffer and StringBuilder class

StringBuffer and StringBuilder class

When the string changes, and requires the use of StringBuffer StringBuilder class.

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

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

Since the StringBuffer StringBuilder compared to the speed advantage, so in most cases recommend using the StringBuilder class. However, in the case of application requires thread-safe, you must use the StringBuffer class.

Test.java file code:

public class Test{
  public static void main(String args[]){
    StringBuffer sBuffer = new StringBuffer("菜鸟教程官网:");
    sBuffer.append("www");
    sBuffer.append(".runoob");
    sBuffer.append(".com");
    System.out.println(sBuffer);  
  }
}

The above examples compiled results are as follows:

Rookie tutorial official website: www.runoob.com

StringBuffer methods

Here are the main method of StringBuffer class support:

sequence The method described
1 public StringBuffer append (String s) specified string to this character sequence.
2 public StringBuffer reverse () this character sequence replaced by the reverse form.
3 public delete (int start, int end) of this sequence to remove the substring in characters.
4 public insert (int offset, int i) int the string representation of the argument into this sequence.
5 replace (int start, int end, String str) given String substring of characters replacement of this sequence of characters.

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

No. The method described
1 int capacity () Returns the current capacity.
2 char charAt (int index) Returns the char value in this sequence index.
3 void ensureCapacity (int minimumCapacity) ensure at least equal to the specified minimum capacity.
4 void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin) from the sequence of characters copied to the target 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, returns the first occurrence of the specified index of the string in the string.
7 int lastIndexOf (String str) Returns the specified sub-string of the rightmost occurrence of the index within this string.
8 int lastIndexOf (String str, int fromIndex) Returns a String object position of the last occurrence.
9 int length () Returns the length (number of characters).
10 Character Set void setCharAt (int index, char ch) at the specified index of 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 sequence.
15 String toString () Returns a string representation of the data sequence.

Click here to see the realization method

Guess you like

Origin www.cnblogs.com/jason1999/p/11682131.html