Android 中的 String, StringBuffer 和 StringBuilder 是移動手機開發中經常使用到的字符串類。做為基礎知識是必須要理解的,這里做一些總結。
A、區別
可以從以下兩個方面來理解
1、不變和可變。
String類:使用數組保存字符串,類中的定義如下:
private final char value[];
注意到,因為用了final修飾符,所以可以知道string對象是長度不可變的。
StringBuilder和StringBuffer類:都是繼承自AbstractStringBuilder類,在AbstractStringBuilder中也是使用動態的字符數組保存字符串,定義如下:
char[] value
注意到,未用到final修飾符,故可知這兩種對象都是可變的。
2、是否線程安全。
String的對象是不可變的,因此它是線程安全的。
StringBuffer對方法加了同步鎖或者對調用的方法加了同步鎖,所以線程是安全的。如下代碼所示:
1 public synchronized StringBuffer reverse() { 2 super.reverse(); 3 return this; 4 }
StringBuilder對方法沒有進行同步鎖,所以是非線程安全的。
注意:AbstractStringBuilder是StringBuilder和StringBuffer類的公共父類,它定義了一些字符串的基本操作的共同方法。
B、共同點
1、在多線程環境下,StringBuffer的效率要比StringBuilder低。
2、抽象類AbstractStringBuilder是StringBuilder和StringBuffer類的公共父類。他們都調用父類的公共方法。如supper.append(...);
C、屬性方法和使用
String的常用公共方法:
public int lenght() | 返回字符串的長度。 |
public char charAt(int index) | 返回字符串位置index處的字符。 |
public boolean equals(Object o) | 比較兩個字符串對象,相等則返回true;反之返回false。 |
public int compareTo(String s) | 比較兩個字符串字典順序,相等返回0,s大於當前字符串返回一個負值,s小於當前串返回一個正值。 |
public boolean regionMatches(int toffset,String other,int ooffset,int len) | 從當前字符串位置toffset開始尋找字符串other中起始位置為ooffset 長度為len 的子串。如發現匹配,返回true; 否則,返回false。 |
public boolean startsWith(String prefix) | 從當前字符串的起始位置開始尋找字符串 prefix。如發現匹配,返回true;否則,返回false。 |
public boolean endsWith(String suffix) | 如當前字符串的結尾子串與 suffix 匹配,返回true;否則,返回false。 |
public int indexOf(String str) | 在當前字符串中尋找與str匹配的子串,返回首次匹配的起始下表值;無匹配返回-1。 |
public String substring(int beginIndex,int endIndex) | 在當前字符串中,求從起始位置 beginIndex 到結束位置 endIndex 的子串。 |
public String concat(String str) | 將當前字符串與str連接,返回連接后的字符串。 |
public String toLowerCase() | 將當前字符串全轉換為小寫形式。 |
public String toUpperCase() | 將當前字符串轉換為大寫形式。 |
public char toCharArray() | 將當前字符串轉換為字符數組。 |
public Static String valueOf(type variable) | 把variable 轉換為字符串,其中 type 表示 variable 的數據類型。 |
字符串連接的方法:
1 String a = "abc" 2 String b = "xkk" 3 b +=a;
StringBuffer的常用公共方法
public int length() | 返回緩沖區的字符數 |
public int capacity() | 返回緩沖區的容量大小,其值為:字符串長度+16。 |
public synchronized StringBuffer append(type variable) | 把variable轉換為字符串,然后與當前字符串連接。 |
public synchronized StringBuffer append(Char(char ch) | 把字符ch連接到當前串尾。 |
public synchronized StringBuffer insert(int offset,type variable) | 把variable轉換為字符串,然后插入到當前串中由offset指定的位置。 |
public synchronized StringBuffer insert(int offset,char ch) | 把字符 ch 插入到當前串由ofset指定的位置。 |
public synchronized String toString() | 把StringBuffer轉換為字符串String。 |
字符串連接方法
1 StringBuffer str= new StringBuffer("kkkxxx."); 2 str.append("xkk.");
StringBuilder的常用公共方法
StringBuilder | append(boolean b) |
Appends the string representation of the boolean argument to the sequence. | |
StringBuilder | append(char c) |
Appends the string representation of the char argument to this sequence. | |
StringBuilder | append(char[] str) |
Appends the string representation of the char array argument to this sequence. | |
StringBuilder | append(char[] str, int offset, int len) |
Appends the string representation of a subarray of the char array argument to this sequence. | |
StringBuilder | append(CharSequence s) |
向此 Appendable 添加指定的字符序列。 | |
StringBuilder | append(CharSequence s, int start, int end) |
Appends a subsequence of the specified CharSequence to this sequence. | |
StringBuilder | append(double d) |
Appends the string representation of the double argument to this sequence. | |
StringBuilder | append(float f) |
Appends the string representation of the float argument to this sequence. | |
StringBuilder | append(int i) |
Appends the string representation of the int argument to this sequence. | |
StringBuilder | append(long lng) |
Appends the string representation of the long argument to this sequence. | |
StringBuilder | append(Object obj) |
Appends the string representation of the Object argument. | |
StringBuilder | append(String str) |
Appends the specified string to this character sequence. | |
StringBuilder | append(StringBuffer sb) |
將指定的 StringBuffer 添加到此序列。 | |
StringBuilder | appendCodePoint(int codePoint) |
Appends the string representation of the codePoint argument to this sequence. | |
int | capacity() |
Returns the current capacity. | |
char | charAt(int index) |
Returns the char value in this sequence at the specified index. | |
int | codePointAt(int index) |
Returns the character (Unicode code point) at the specified index. | |
int | codePointBefore(int index) |
Returns the character (Unicode code point) before the specified index. | |
int | codePointCount(int beginIndex, int endIndex) |
Returns the number of Unicode code points in the specified text range of this sequence. | |
StringBuilder | delete(int start, int end) |
Removes the characters in a substring of this sequence. | |
StringBuilder | deleteCharAt(int index) |
Removes the char at the specified position in this sequence. | |
void | ensureCapacity(int minimumCapacity) |
Ensures that the capacity is at least equal to the specified minimum. | |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) |
Characters are copied from this sequence into the destination character array dst. | |
int | indexOf(String str) |
Returns the index within this string of the first occurrence of the specified substring. | |
int | indexOf(String str, int fromIndex) |
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. | |
StringBuilder | insert(int offset, boolean b) |
Inserts the string representation of the boolean argument into this sequence. | |
StringBuilder | insert(int offset, char c) |
Inserts the string representation of the char argument into this sequence. | |
StringBuilder | insert(int offset, char[] str) |
Inserts the string representation of the char array argument into this sequence. | |
StringBuilder | insert(int index, char[] str, int offset, int len) |
Inserts the string representation of a subarray of the str array argument into this sequence. | |
StringBuilder | insert(int dstOffset, CharSequence s) |
Inserts the specified CharSequence into this sequence. | |
StringBuilder | insert(int dstOffset, CharSequence s, int start, int end) |
Inserts a subsequence of the specified CharSequence into this sequence. | |
StringBuilder | insert(int offset, double d) |
Inserts the string representation of the double argument into this sequence. | |
StringBuilder | insert(int offset, float f) |
Inserts the string representation of the float argument into this sequence. | |
StringBuilder | insert(int offset, int i) |
Inserts the string representation of the second int argument into this sequence. | |
StringBuilder | insert(int offset, long l) |
Inserts the string representation of the long argument into this sequence. | |
StringBuilder | insert(int offset, Object obj) |
Inserts the string representation of the Object argument into this character sequence. | |
StringBuilder | insert(int offset, String str) |
Inserts the string into this character sequence. | |
int | lastIndexOf(String str) |
Returns the index within this string of the rightmost occurrence of the specified substring. | |
int | lastIndexOf(String str, int fromIndex) |
Returns the index within this string of the last occurrence of the specified substring. | |
int | length() |
Returns the length (character count). | |
int | offsetByCodePoints(int index, int codePointOffset) |
Returns the index within this sequence that is offset from the given index by codePointOffset code points. | |
StringBuilder | replace(int start, int end, String str) |
Replaces the characters in a substring of this sequence with characters in the specified String. | |
StringBuilder | reverse() |
Causes this character sequence to be replaced by the reverse of the sequence. | |
void | setCharAt(int index, char ch) |
The character at the specified index is set to ch. | |
void | setLength(int newLength) |
Sets the length of the character sequence. | |
CharSequence | subSequence(int start, int end) |
Returns a new character sequence that is a subsequence of this sequence. | |
String | substring(int start) |
Returns a new String that contains a subsequence of characters currently contained in this character sequence. | |
String | substring(int start, int end) |
Returns a new String that contains a subsequence of characters currently contained in this sequence. | |
String | toString() |
Returns a string representing the data in this sequence. | |
void | trimToSize() |
Attempts to reduce storage used for the character sequence. |
字符串連接方法
1 StringBuilder sb2 = new StringBuilder(); 2 sb2.append( "xxxkkk" );
D、參考資料
1、http://www.cnblogs.com/xudong-bupt/p/3961159.html(java中String、StringBuffer、StringBuilder的區別)
2、http://www.apihome.cn/api/java/StringBuilder.html(StringBuilder(示例,出錯代碼))
3、http://blog.csdn.net/zhzhl29290029/article/details/12339981(黑馬程序--JAVA字符串String、StringBuffer、StringBuilder、基本數據類型包裝)