方法 | 描述 |
---|---|
int compareToIgnoreCase(String str) | 逐字元比較原字串與 anotherString ,不考慮大小寫的分別,若無不同,也就是兩個字串中的每個字元都相同就回傳 0 ,若有不同則回傳長度差或第一個不同字元的 Unicode 差值 |
內文依據花生狼網友的指正於 2011, 8, 26 進行修改,重寫之範例說明,請參考關於 compareTo() 的補充範例說明。
舉例如下
class ComparetoignorecaseDemo { public static void main(String[] args) { String a = "Imagination is more important than knowledge."; String b = "He who has hope has everything."; String c = "台上一分鐘,台下十年功。"; String d = "imagination is more important than knowledge."; String e = "He who has hope has everything"; String f = "台上十分鐘,台下一年功。"; System.out.println(a.compareToIgnoreCase(d)); System.out.println(b.compareToIgnoreCase(e)); System.out.println(c.compareToIgnoreCase(f)); } } /* 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:ComparetoignorecaseDemo.java 功能:示範物件導向的基本觀念 作者:張凱慶 時間:西元 2010 年 10 月 */
編譯後執行,結果如下
中英文術語對照 | |
---|---|
類別 | class |
方法 | method |
字串 | string |
參考資料
http://download.oracle.com/javase/tutorial/java/data/strings.html
http://download.oracle.com/javase/tutorial/java/data/converting.html
http://download.oracle.com/javase/tutorial/java/data/manipstrings.html
http://download.oracle.com/javase/tutorial/java/data/comparestrings.html
http://download.oracle.com/javase/7/docs/api/java/lang/String.html
http://download.oracle.com/javase/tutorial/java/data/strings.html
http://download.oracle.com/javase/tutorial/java/data/converting.html
http://download.oracle.com/javase/tutorial/java/data/manipstrings.html
http://download.oracle.com/javase/tutorial/java/data/comparestrings.html
http://download.oracle.com/javase/7/docs/api/java/lang/String.html
2 則留言:
當然啦,compareToIgnoreCase()也是需要修改的~~
內文已修改,感謝指正 ^^
張貼留言