String中常用的方法

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決定是否忽略大小寫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章