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

2021-08-20 01:46:48 字數 4338 閱讀 9714

13.1 不可變string##

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

public class immutable 

public static void main(string args)

/**輸出

* howdy

* howdy

* howdy

*/}

13.2 過載"+"與stringbuilder##

"+"的底層實現方式也是stringbuilder,後者更加高效。

"+"連線符會生成很多中間物件,需要**。

13.3 無意識的遞迴##

/**

* 無意識遞迴

* @author administrator

* */

public class infiniterecursion

public static void main(string args)

system.out.println(v);

}}

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

13.4 string上的操作##

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

sn(序號)

方法描述

1char charat(int index)

返回指定索引處的 char 值。

2int compareto(object o)

把這個字串和另乙個物件比較。

3int compareto(string anotherstring)

按字典順序比較兩個字串。

4int comparetoignorecase(string str)

按字典順序比較兩個字串,不考慮大小寫。

5string concat(string str)

將指定字串連線到此字串的結尾。

6boolean contentequals(stringbuffer sb)

當且僅當字串與指定的stringbuffer有相同順序的字元時候返回真。

7static string copyvalueof(char data)

返回指定陣列中表示該字串行的 string。

8static string copyvalueof(char data, int offset, int count)

返回指定陣列中表示該字串行的 string。

9boolean endswith(string suffix)

測試此字串是否以指定的字尾結束。

10boolean equals(object anobject)

將此字串與指定的物件比較。

11boolean equalsignorecase(string anotherstring)

將此 string 與另乙個 string 比較,不考慮大小寫。

12byte getbytes()

使用平台的預設字符集將此 string 編碼為 byte 序列,並將結果儲存到乙個新的 byte 陣列中。

13byte getbytes(string charsetname)

使用指定的字符集將此 string 編碼為 byte 序列,並將結果儲存到乙個新的 byte 陣列中。

14void getchars(int srcbegin, int srcend, char dst, int dstbegin)

將字元從此字串複製到目標字元陣列。

15int hashcode()

返回此字串的雜湊碼。

16int indexof(int ch)

返回指定字元在此字串中第一次出現處的索引。

17int indexof(int ch, int fromindex)

返回在此字串中第一次出現指定字元處的索引,從指定的索引開始搜尋。

18int indexof(string str)

返回指定子字串在此字串中第一次出現處的索引。

19int indexof(string str, int fromindex)

返回指定子字串在此字串中第一次出現處的索引,從指定的索引開始。

20string intern()

返回字串物件的規範化表示形式。

21int lastindexof(int ch)

返回指定字元在此字串中最後一次出現處的索引。

22int lastindexof(int ch, int fromindex)

返回指定字元在此字串中最後一次出現處的索引,從指定的索引處開始進行反向搜尋。

23int lastindexof(string str)

返回指定子字串在此字串中最右邊出現處的索引。

24int lastindexof(string str, int fromindex)

返回指定子字串在此字串中最後一次出現處的索引,從指定的索引開始反向搜尋。

25int length()

返回此字串的長度。

26boolean matches(string regex)

告知此字串是否匹配給定的正規表示式。

27boolean regionmatches(boolean ignorecase, int toffset, string other, int ooffset, int len)

測試兩個字串區域是否相等。

28boolean regionmatches(int toffset, string other, int ooffset, int len)

測試兩個字串區域是否相等。

29string replace(char oldchar, char newchar)

返回乙個新的字串,它是通過用 newchar 替換此字串中出現的所有 oldchar 得到的。

30string replaceall(string regex, string replacement)

使用給定的 replacement 替換此字串所有匹配給定的正規表示式的子字串。

31string replacefirst(string regex, string replacement)

使用給定的 replacement 替換此字串匹配給定的正規表示式的第乙個子字串。

32string split(string regex)

根據給定正規表示式的匹配拆分此字串。

33string split(string regex, int limit)

根據匹配給定的正規表示式來拆分此字串。

34boolean startswith(string prefix)

測試此字串是否以指定的字首開始。

35boolean startswith(string prefix, int toffset)

測試此字串從指定索引開始的子字串是否以指定字首開始。

36charsequence subsequence(int beginindex, int endindex)

返回乙個新的字串行,它是此序列的乙個子串行。

37string substring(int beginindex)

返回乙個新的字串,它是此字串的乙個子字串。

38string substring(int beginindex, int endindex)

返回乙個新字串,它是此字串的乙個子字串。

39char tochararray()

將此字串轉換為乙個新的字元陣列。

40string tolowercase()

使用預設語言環境的規則將此 string 中的所有字元都轉換為小寫。

41string tolowercase(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為小寫。

42string tostring()

返回此物件本身(它已經是乙個字串!)。

43string touppercase()

使用預設語言環境的規則將此 string 中的所有字元都轉換為大寫。

44string touppercase(locale locale)

使用給定 locale 的規則將此 string 中的所有字元都轉換為大寫。

45string trim()

返回字串的副本,忽略前導空白和尾部空白。

46static string valueof(primitive data type x)

返回給定data type型別x引數的字串表示形式。

《Java程式設計思想》讀書筆記 第十三章 字串

string物件是不可變的,每乙個看起來會修改string值的方法,實際上都是建立乙個全新的string物件,以及包含修改後的字串內容,而最初的string物件則絲毫未動 換句話說 任何對string的改變都會引發新的物件的生成stringbuilder 執行緒不安全的 stringbuffer 執...

java第十三章總結

url url new url url物件呼叫inputstream openstream 方法返回乙個輸入流 獲取位址 inetaddress類的靜態方法 getbyname string s gethostname 獲得inetaddress物件所含的網域名稱 gethostaddress 獲得...

c primer plus 第十三章課後程式設計6題

include include include define len 40 int main void 開啟檔案並輸入內容 if in fopen name,w null printf 請輸入檔案內容 n while ch getc stdin eof putc ch,in if fclose in...