1 / 70

第 10 章

第 10 章. 字串. 字串的產生. test. 4ED6. 4ED6. test[0]. test[1]. test[2]. 其中 StringBuffer 與 StringBuilder 類別會在 10-3 節中介紹。底下就來看看 如何透過前 4 個建構方法產生字串 :. test[3]. test[4]. b. test[5]. c. 6124. a. 6124. 6255. 5F12. test[6]. 5F12. d. 63EF. 63EF. 字串的產生. a. 5F12. 5F12. b. d. c.

Télécharger la présentation

第 10 章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第10章 字串

  2. 字串的產生 test 4ED6 4ED6 test[0] test[1] test[2] • 其中 StringBuffer 與 StringBuilder 類別會在 10-3 節中介紹。底下就來看看如何透過前 4 個建構方法產生字串: test[3] test[4] b test[5] c 6124 a 6124 6255 5F12 test[6] 5F12 d 63EF 63EF

  3. 字串的產生 a 5F12 5F12 b d c 6124 63EF 6124 6255 63EF

  4. 使用字面常數建立 String 物件 • Java 對於 String 類別最重要的支援, 除了可以用 + 號來連接字串之外, 還可以使用字面常數來產生 String 物件, 例如: a a c c 64AC 6124 6124 b b 6124 6124 64AC 6124 c 是建立副本, 指向另一個新物件。 執行結果:

  5. 使用字面常數建立 String 物件 • 其中第 4 行就是直接使用字面常數建立物件。當程式中有字面常數時, Java 編譯器其實會產生一個 String 物件來代表所有相同內容的字面常數字串。 • 也就是說, 第 5 行設定給 b 的參照值其實和給 a 的是一樣的, 都指向同一個String 物件;而第 6 行傳給 String 類別建構方法的也是同一個物件。 • 您可以把這 3 行看成是這樣: c constant1 64AC 6124 a 64AC 6124 b 6124

  6. 使用字面常數建立 String 物件 a 6124 b 6124 6124 c 64AC 64AC

  7. 自動轉型 (Implicit Conversion) a 4ED6 4ED6 相當於寫成: a.toString 4F18 4F18

  8. 自動轉型 (Implicit Conversion) • 要注意的是, toString() 方法必須傳回 String 物件, 而且必須加上 public 存取控制。 • 若是字串與基本資料型別的變數做連接運算, 則該變數會被包裝成對應類別的物件, 再呼叫該類別的 toString() 方法, 詳見第 11-4-2 節。 int k = 20; System.out.println(“ k 值為 “+ k ); 會被包裝成Integer類別的物件

  9. char charAt(int index) a 64AC 索引碼 0 1 2 3 4 5 6 7 這 這 物件名稱.charAt(int) 練習: System.out.println(“Test : ” + “大華技術學院”.charAt(5));

  10. int compareTo(String anotherString) • 以逐字元方式 (Lexically)與 anotherString 所指字串的內容比較, 如果 anotherString 比較大, 就傳回一個負數值。 • 如果字串內容完全相同, 就傳回0;如果 anotherString 比較小, 就傳回一個正數值。

  11. int compareTo(String anotherString) 事實上, 在比較時是做兩個字母的Unicode相減的動作。 a Unicode

  12. int compareTo(String anotherString) • 與 equals()方法類似, compareTo() 方法也有一個雙胞胎 compareToIgnoreCase(), 在比較時會將同一字母大小寫視為相同。 練習: 將上述程式第6行下面加上以下兩敘述: System.out.println(a.compareTo(“ABCD”)); System.out.println(a.compareToIgnoreCase(“ABCD”));

  13. boolean contains(CharSequence s) • 傳回字串中是否包含有 s 所指字串的內容在裡頭。 練習: 將上述程式第8行下面加上以下兩敘述: System.out.println(a.contains(“acd”)); System.out.println(a.contains(“ABCD”));

  14. boolean endsWith(String suffix) • 傳回是否以指定的字串內容結尾。 練習: 將上述程式第5行下面加上以下兩敘述: System.out.println(a.endsWith(“abcd”)); System.out.println(a.endsWith(“D”));

  15. void getChars(int srcBegin, int srcEnd, char[ ] dst, int dstBegin) • 將索引碼 srcBegin 到 srcEnd - 1 的字元, 複製到 dst 所指字元陣列、由索引碼 dstBegin 開始的元素中。 srcBegin : source begin 來源開始 srcEnd : source end 來源結束 dst : destination 目的 dstBegin : destination begin 目的開始 chars chars a a 索引碼 Chars[0] 0 Chars[1] 1 複製 Chars[2] Chars[2] 2 3 練習: 試改成 System.out.println( chars ) ; Chars[3] Chars[3] 4 5 6 7

  16. int indexOf(int ch) • 傳回 ch 所指定的字元在字串中第一次出現位置的索引碼, 如果字串中未包含該字元, 就傳回 -1。 a 索引碼 0 1 2 3 4 5 6 7

  17. int indexOf(int ch) • 這個方法有個雙胞胎的版本, 叫做 lastIndexOf(), 可以從字串尾端往前尋找。 練習: 將上述程式第4行改成: String b = “這是一個個測試字串”; 第6行下面加上以下敘述: System.out.println(b.lastIndexOf(‘個’)); b 索引碼 0 1 2 3 4 5 6 7 8

  18. int indexOf(int ch, int fromIndex) • indexOf() 方法的多重定義版本, 可以透過 fromIndex 指定開始尋找的位置。 • 只要結合這 2 種 indexOf() 方法, 就可以逐一找出字串中所有出現指定字元的位置了。 • 這個方法也有個雙胞胎的版本, 叫做 lastIndexOf(), 可以從字串尾端往前尋找。 b 索引碼 練習: 將上述改過的程式最後加上以下敘述: System.out.println(b.indexOf(‘個’,2)); System.out.println(b.lastIndexOf(‘個’,6)); 0 1 2 3 4 5 6 7 8

  19. int indexOf(String str) • indexOf() 的多重定義版本, 尋找的是指定字串出現的位置。 a 索引碼 0 1 2 3 4 找不到”字符” 5 6 7

  20. int indexOf(String str) • 這個方法也有個雙胞胎的版本, 叫做lastIndexOf(), 可以從字串尾端往前尋找。 a 索引碼 練習: 將上述程式最後加上以下敘述: String b = “這是一個一個測試字串” System.out.println(b.lastIndexOf(“一個”)); 0 1 2 3 4 5 6 7 8 9

  21. int indexOf(String str, int fromIndex) • indexOf() 方法的多重定義版本, 可以透過 fromIndex 指定開始尋找的位置。 • 只要結合這 2 種 indexOf() 方法, 就可以逐一找出所有出現指定字串的位置了。 • 當然也有個對應的 lastIndexOf()方法, 可以從字串尾端往前尋找。 b 索引碼 0 1 練習: 將上述程式最後加上: String b = “這是一個一個測試字串”; System.out.println(b.indexOf(“一個”,1 )); System.out.println(b.lastIndexOf(“一個”,7)); System.out.println(b.indexOf(“一個”,5)); 2 3 4 5 6 7 8 9

  22. int length() • 傳回字串的長度。 b 索引碼 練習: 將上述程式最後加上: System.out.println(b.length()); 0 1 2 方法 3 4 5 6 10 7 8 9 char[] a = new char[10]; System.out.println(a.length); 屬性

  23. String replace(char oldChar, char newChar) • 將字串中所有出現 oldChar 所指定的字元取代為由 newChar 所指定的字元。 • 要提醒您的是, 這並不會更改原始字串的內容, 而是將取代的結果以新的字串傳回。 練習: 修改此行敘述為 String a = “這是一個測試測字串“; a 索引碼 0 1 2 3 考 4 5 6 7

  24. String replace(CharSequence target, CharSequence replacement) • 和上一個方法功能類似, 但是將字串中所有出現 target 所指字串內容的地方都取代為 replacement 所指字串的內容。 a 索引碼 0 1 2 3 正式 4 5 6 7

  25. boolean startsWith(String prefix)boolean startsWith(String prefix, int offset) • startsWith() 的用法和前面看過的 endsWith() 類似, 但功能相反, startsWith() 是用來檢查目前字串是否是以參數字串 prefix 開頭。 • 較特別的是 startsWith() 有兩個參數的版本, 可指定從索引位置 offset 開始, 檢查是否以參數字串 prefix 為開頭。

  26. boolean startsWith(String prefix)boolean startsWith(String prefix, int offset) a 索引碼 0 1 2 3

  27. String substring(int beginIndex) • 傳回由 beginIndex 所指定索引開始到結尾的子字串。

  28. String substring(int beginIndex, int endIndex) • 傳回由 beginIndex所指定的索引碼開始到 endIndex - 1所指定的索引碼為止的部分字串。 a 索引碼 0 1 2 3 4 5 6 7

  29. String toLowerCase() • 傳回將字串中的字元轉成小寫後的副本。 練習: 將上述程式最後加上: String b = “AbcdE”; System.out.println(b.toLowerCase());

  30. String toUpperCase() • 將字串中的字元全部轉為大寫。 練習: 將上述程式最後加上: System.out.println(“AbcdE”.toUpperCase());

  31. String trim() • 將字串中頭、尾端的空白符號去除,包含空白字元、定位字元等等。 用一個 代表一個 空白。 △ △ △ △ △ △ …

  32. String trim() • 以上這些方法都經常會用到, 也是應考的重點。 練習: 將上述程式第5行改成: System.out.println(“測試” + a.trim() + “trim()方法”);

  33. StringBuffer 類別 a b 索引碼 索引碼 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7

  34. StringBuffer 類別 • 還記得在 10-5 頁提過, Java 會產生一個 String 物件來代替程式中的字面常數, 所以第 4、5 行也可直接寫成: • 以下就來介紹 StringBuffer 類別的修改字串方法, 這些方法不但會直接修改 StringBuffer 物件的內容, 也會將修改後的結果傳回。 String a = “這是一個測試字串”; StringBuffer b = new StringBuffer(a) ;

  35. append() 方法 append()方法擁有多重定義的版本, 可以傳入基本型別、String 物件以及其他有定義toString()方法的物件。 a a a b

  36. insert() 方法 • insert() 方法和 append() 方法一樣有多種版本( Ref. http://java.sun.com/ ), 但是它可以透過第 1 個參數 offset將第 2 個參數插入到字串中的特定位置。 • offset代表的是索引碼, insert() 方法會把資料插入到 offset 所指的位置之前。 a b 索引碼 索引碼 索引碼 索引碼 0 1 1 1 1 2 2 2 2 3 4 4 4 4 5 5 6 6 6 6 7

  37. insert() 方法 • 在第 11 行可以看到, 如果第 1 個參數傳入字串的長度, 就等於是 append()了。 a b 18 索引碼 2 3 4 5 6 0 7 1 0 1 2 3 4 b 索引碼 17 b b

  38. StringBuffer delete(int start, int end) • delete() 方法可以刪除由 start 所指定索引碼開始到 end - 1所指定索引碼之間的一段字元。 a b 索引碼 3 4 0 1 2 5 6 7 相當於 b.deleteCharAt(1) (參考下一張投影片) b 索引碼 4 5 3 0 1 2 6

  39. StringBuffer deleteCharAt(int index) • 刪除由 index 所指定索引碼的字元。

  40. StringBuffer replace(int start, int end, String str) • 將 start 所指定索引碼開始到 end - 1 所指定索引碼之間的一段字元取代為 str 所指定的字串。 b 索引碼 4 3 0 1 2 5 6 7 b 索引碼 4 2 3 0 1 5 6 取代

  41. StringBuffer reverse() • 將整個字串的內容頭尾反轉。例如: 串字試測 測試字串

  42. void setCharAt(int index, char ch) • 將 index 所指定索引碼的字元取代成 ch 所指定的字元。 • 請特別注意, 這是唯一一個更改了字串內容, 但卻沒有傳回自己的方法, 在使用時要特別小心。

  43. void setCharAt(int index, char ch) 二 b 索引碼 4 3 6 0 2 5 7 1

  44. 甚麼是規則表示法 str

  45. 甚麼是規則表示法 Unicode str 索引碼 1 3 0 2 4

  46. 甚麼是規則表示法 str 索引碼 2 3 0 1

  47. 10-4-2 規則表示法入門 • 為了讓大家可以直接練習, 所以我們先撰寫了一個測試的程式, 可以讓您直接輸入樣式以及要比對的字串, 並顯示出比對的結果是否相符:

  48. 規則表示法入門 (輸入樣式) pat 索引碼 2 3 4 0 1 (輸入字串) str print Print 索引碼 2 3 4 0 1

  49. 限制出現次數 • 除了剛剛使用過的 "+" 以外, 規則表示法中還可以使用如下常用的次數限制規則 (量詞): 出現兩次

  50. 字元種類 (Character Classes) • 您也可以用中括號來表示一組字元, 比如說: • 其中樣式 [bjl] 表示此位置可以出現 'b' 或 'j' 或 'l', 因此 "a[bjl]a" 這個樣式的意思就是先出現一個 'a', 再出現一個 'b' 或 'j' 或 'l ', 再接著一個 'a'。 • 在第 2個執行結果中, 因為輸入的字串第 2 個字元並非 'b' 或 'j' 或 'l', 所以不相符。

More Related