String的源碼分析
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
上面爲String的定義:
1、字符串比較
equals() ------判斷內容是否相同。
compareTo() ------判斷字符串的大小關係。
compareToIgnoreCase(String int) ------在比較時忽略字母大小寫。
== ------判斷內容與地址是否相同。
equalsIgnoreCase() ------忽略大小寫的情況下判斷內容是否相同。
reagionMatches() ------對字符串中的部分內容是否相同進行比較(詳情請參考API)。
2、字符串查找
charAt(int index) ------返回指定索引index位置上的字符,索引範圍從0開始。
indexOf(String str)------從字符串開始檢索str,並返回第一次出現的位置,未出現返回-1。
indexOf(String str,int fromIndex);------從字符串的第fromIndex個字符開始檢索str。
lastIndexOf(String str)------查找最後一次出現的位置。
lastIndexOf(String str,int fromIndex)----從字符串的第fromIndex個字符查找最後一次出現的位置。
starWith(String prefix,int toffset)-----測試此字符串從指定索引開始的子字符串是否以指定前綴開始。
starWith(String prefix)------測試此字符串是否以指定的前綴開始。
endsWith(String suffix)------測試此字符串是否以指定的後綴結束。
3、字符串截取
public String subString(int beginIndex)------返回一個新的字符串,它是此字符串的一個子字符串。
public String subString(int beginIndex,int endIndex)------返回的字符串是從beginIndex開始到endIndex-1的串。
4、字符串替換
public String replace(char oldChar,char newChar)。
public String replace(CharSequence target,CharSequence replacement)------把原來的etarget子序列替換爲replacement序列,返回新串。
public String replaceAll(String regex,String replacement)------用正則表達式實現對字符串的匹配。注意replaceAll第一個參數爲正則表達式。