java String方法列表

來源:互聯網
上載者:User
JAVA String方法列表

1、length() 字串的長度
  例:char chars[]={'a','b'.'c'};
    String s=new String(chars);
    int len=s.length();

2、charAt() 截取一個字元
  例:char ch;
    ch="abc".charAt(1); 返回'b'

3、 getChars() 截取多個字元
  void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
  sourceStart指定了子串開始字元的下標,sourceEnd指定了子串結束後的下一個字元的下標。因此, 子串包含從sourceStart到sourceEnd-1的字元。接收字元的數組由target指定,target中開始複製子串的下標值是targetStart。
  例:String s="this is a demo of the getChars method.";
    char buf[]=new char[20];
    s.getChars(10,14,buf,0);

4、getBytes()
  替代getChars()的一種方法是將字元儲存在位元組數組中,該方法即getBytes()。

5、toCharArray()

6、equals()和equalsIgnoreCase() 比較兩個字串

7、regionMatches() 用於比較一個字串中特定地區與另一特定地區,它有一個重載的形式允許在比較中忽略大小寫。
  boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
  boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)

8、startsWith()和endsWith()
  startsWith()方法決定是否以特定字串開始,endWith()方法決定是否以特定字串結束

9、equals()和==
  equals()方法比較字串對象中的字元,==運算子比較兩個對象是否引用同一執行個體。
  例:String s1="Hello";
    String s2=new String(s1);
    s1.eauals(s2); //true
    s1==s2;//false

10、compareTo()和compareToIgnoreCase() 比較字串

11、indexOf()和lastIndexOf()
  indexOf() 尋找字元或者子串第一次出現的地方。
  lastIndexOf() 尋找字元或者子串是後一次出現的地方。

12、substring()
  它有兩種形式,第一種是:String substring(int startIndex)
         第二種是:String substring(int startIndex,int endIndex)

13、concat() 串連兩個字串

14 、replace() 替換
  它有兩種形式,第一種形式用一個字元在調用字串中所有出現某個字元的地方進行替換,形式如下:
  String replace(char original,char replacement)
  例如:String s="Hello".replace('l','w');
  第二種形式是用一個字元序列替換另一個字元序列,形式如下:
  String replace(CharSequence original,CharSequence replacement)

15、trim() 去掉起始和結尾的空格

16、valueOf() 轉換為字串

17、toLowerCase() 轉換為小寫

18、toUpperCase() 轉換為大寫

19、StringBuffer建構函式
  StringBuffer定義了三個建構函式:
  StringBuffer()
  StringBuffer(int size)
  StringBuffer(String str)
  StringBuffer(CharSequence chars)
  
  (1)、length()和capacity()
    一個StringBuffer當前長度可通過length()方法得到,而整個可分配空間通過capacity()方法得到。
  
  (2)、ensureCapacity() 設定緩衝區的大小
    void ensureCapacity(int capacity)

  (3)、setLength() 設定緩衝區的長度
    void setLength(int len)

  (4)、charAt()和setCharAt()
    char charAt(int where)
    void setCharAt(int where,char ch)

  (5)、getChars()
    void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)

  (6)、append() 可把任何類型資料的字串表示串連到調用的StringBuffer對象的末尾。
    例:int a=42;
      StringBuffer sb=new StringBuffer(40);
      String s=sb.append("a=").append(a).append("!").toString();

  (7)、insert() 插入字串
    StringBuffer insert(int index,String str)
    StringBuffer insert(int index,char ch)
    StringBuffer insert(int index,Object obj)
    index指定將字串插入到StringBuffer對象中的位置的下標。

  (8)、reverse() 顛倒StringBuffer對象中的字元
    StringBuffer reverse()

  (9)、delete()和deleteCharAt() 刪除字元
    StringBuffer delete(int startIndex,int endIndex)
    StringBuffer deleteCharAt(int loc)

  (10)、replace() 替換
    StringBuffer replace(int startIndex,int endIndex,String str)

  (11)、substring() 截取子串
    String substring(int startIndex)
    String substring(int startIndex,int endIndex)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.