String中常用的方法

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.