1. 程式人生 > >《java程式設計思想——第十三章(字串)》

《java程式設計思想——第十三章(字串)》

字串##

13.1 不可變String##

string物件是不可變。String類中每一個看起來會修改String值得方法,實際上都建立了一個新的String物件。

public class Immutable {;

	public static String upcase (String s) {
		
		return s.toUpperCase();
	}
	public static void main(String[] args) {
		String q = "howdy";
		Print.print(q);
		String qq = upcase(q);
		Print.print(qq);
		Print.print(q);
	}
	/**輸出
	 * howdy
	 * HOWDY
	 * howdy
	 */
}

13.2 過載"+"與StringBuilder##

"+"的底層實現方式也是StringBuilder,後者更加高效。
"+"連線符會生成很多中間物件,需要回收。

13.3 無意識的遞迴##

/**
 * 無意識遞迴
 * @author Administrator
 *
 */
public class InfiniteRecursion {

	public String toString() {
		return "InfiniteRecursion address:"+this+"\n";
	}
	public static void main(String[] args) {
		List<InfiniteRecursion> v = 
		new ArrayList<InfiniteRecursion>();
		for (int i = 0; i < 10; i++) {
			v.add(new InfiniteRecursion());
		}
		System.out.println(v);
	}
}

在String後跟"+"時會嘗試把this轉換成String型別,去呼叫this 的toString()方法,於是就發生了遞迴呼叫。

13.4 String上的操作##

在需要改變字串內容時,返回一個新字串;不改變內容時返回原字串的引用。

SN(序號) 方法描述
1 char charAt(int index)
返回指定索引處的 char 值。
2 int compareTo(Object o)
把這個字串和另一個物件比較。
3 int compareTo(String anotherString)
按字典順序比較兩個字串。
4 int compareToIgnoreCase(String str)
按字典順序比較兩個字串,不考慮大小寫。
5 String concat(String str)
將指定字串連線到此字串的結尾。
6 boolean contentEquals(StringBuffer sb)
當且僅當字串與指定的StringBuffer有相同順序的字元時候返回真。
7 static String copyValueOf(char[] data)
返回指定陣列中表示該字元序列的 String。
8 static String copyValueOf(char[] data, int offset, int count)
返回指定陣列中表示該字元序列的 String。
9 boolean endsWith(String suffix)
測試此字串是否以指定的字尾結束。
10 boolean equals(Object anObject)
將此字串與指定的物件比較。
11 boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 比較,不考慮大小寫。
12 byte[] getBytes()
 使用平臺的預設字符集將此 String 編碼為 byte 序列,並將結果儲存到一個新的 byte 陣列中。
13 byte[] getBytes(String charsetName)
使用指定的字符集將此 String 編碼為 byte 序列,並將結果儲存到一個新的 byte 陣列中。
14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字元從此字串複製到目標字元陣列。
15 int hashCode()
返回此字串的雜湊碼。
16 int indexOf(int ch)
返回指定字元在此字串中第一次出現處的索引。
17 int indexOf(int ch, int fromIndex)
返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。
18 int indexOf(String str)
 返回指定子字串在此字串中第一次出現處的索引。
19 int indexOf(String str, int fromIndex)
返回指定子字串在此字串中第一次出現處的索引,從指定的索引開始。
20 String intern()
 返回字串物件的規範化表示形式。
21 int lastIndexOf(int ch)
 返回指定字元在此字串中最後一次出現處的索引。
22 int lastIndexOf(int ch, int fromIndex)
返回指定字元在此字串中最後一次出現處的索引,從指定的索引處開始進行反向搜尋。
23 int lastIndexOf(String str)
返回指定子字串在此字串中最右邊出現處的索引。
24 int lastIndexOf(String str, int fromIndex)
 返回指定子字串在此字串中最後一次出現處的索引,從指定的索引開始反向搜尋。
25 int length()
返回此字串的長度。
26 boolean matches(String regex)
告知此字串是否匹配給定的正則表示式。
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字串區域是否相等。
28 boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字串區域是否相等。
29 String replace(char oldChar, char newChar)
返回一個新的字串,它是通過用 newChar 替換此字串中出現的所有 oldChar 得到的。
30 String replaceAll(String regex, String replacement)
使用給定的 replacement 替換此字串所有匹配給定的正則表示式的子字串。
31 String replaceFirst(String regex, String replacement)
 使用給定的 replacement 替換此字串匹配給定的正則表示式的第一個子字串。
32 String[] split(String regex)
根據給定正則表示式的匹配拆分此字串。
33 String[] split(String regex, int limit)
根據匹配給定的正則表示式來拆分此字串。
34 boolean startsWith(String prefix)
測試此字串是否以指定的字首開始。
35 boolean startsWith(String prefix, int toffset)
測試此字串從指定索引開始的子字串是否以指定字首開始。
36 CharSequence subSequence(int beginIndex, int endIndex)
 返回一個新的字元序列,它是此序列的一個子序列。
37 String substring(int beginIndex)
返回一個新的字串,它是此字串的一個子字串。
38 String substring(int beginIndex, int endIndex)
返回一個新字串,它是此字串的一個子字串。
39 char[] toCharArray()
將此字串轉換為一個新的字元陣列。
40 String toLowerCase()
使用預設語言環境的規則將此 String 中的所有字元都轉換為小寫。
41 String toLowerCase(Locale locale)
 使用給定 Locale 的規則將此 String 中的所有字元都轉換為小寫。
42 String toString()
 返回此物件本身(它已經是一個字串!)。
43 String toUpperCase()
使用預設語言環境的規則將此 String 中的所有字元都轉換為大寫。
44 String toUpperCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字元都轉換為大寫。
45 String trim()
返回字串的副本,忽略前導空白和尾部空白。
46 static String valueOf(primitive data type x)
返回給定data type型別x引數的字串表示形式。