String中常用的方法
1. public char charAt(int index)
返回字串中的index個字元
2. public int length()
返回字串長度
3. public int indexOf(String str)
返回字串中出現str的第一個位置
public int lastIndexOf(String str)
返回字串中出現str的最後一個位置。
4. public Boolean equals(String s):
比較字串與another是否相同
public boolean equalsIgnoreCase(String another)
比較字串與another是否相同(忽略大小寫)
equals()方法比較字串對象中的字元,==運算子比較兩個對象是否引用同一執行個體
5. public String replace(char oldChar, char newChar)
在字串中用newChar替換oldChar
String replace(CharSequence original,CharSequence replacement)
用一個字元序列replacement替換另一個字元序列original
6. public boolean startsWith(String prefix)
判斷字串是否以prefix開頭
7. public boolean endsWith(String suffix)
判斷字串是否以suffix結束
8. public String toUpperCase()
返回該字串的大寫形式
9. public String toLowerCase()
返回該字串的小寫形式
10. public String substring(int beginIndex)
返回該字串從beginIndex開始到結尾的子串
11. public String substring(int beginIndex,int endIndex)
返回該字串從beginIndex開始到endIndex結尾的子串
13. public String trim()
去除字串頭尾的空格
14. public int compareTo(String s):
如果當前字串與s相同,傳回值得0;如果大於s,返回正值;如果小於s,則返回負值
public int compareToIgnore(String s):忽略大小寫比較
15.public Boolean regionMatches (int firstStart,String other,int otherStart,int length):
用於比較一個字串中特定地區與另一特定地區
public Boolean regionMatches (Boolean b, int firstStart, String other, int otherStart, int length)
可以通過參數b決定是否忽略大小寫。