java編程筆記3 字串處理之String類

來源:互聯網
上載者:User
2011-03-01 22:20

String

在java中,沒有內建的字串類型,字串常量是作為String類的對象存在的。(String類的對象是字串常量!!!)內容一旦確定不可更改

構造方法

無建構函式                                                             直接指向一個String對象

String()                                                                      建立一個空的String對象

String(String  str)                                                 利用一個存在的String對象複製一個String對象

String(StringBuffer  buf)                                     利用一個存在的StringBuffer對象建立String對象

String(char c[])                                                     利用存在的字元數組建立String對象          

String(byte[] b ,String encoding)                         利用存在的位元組數組建立String對象

在api中可以查到String的構造方法,和方法摘要,部分如下:

 


一些方法-------


字串串連:public Stringconcat(String str)


擷取字串的長度:public int length()


擷取字串某位置的字元:public char charAt(int index)


字串的比較:

public int compareTo(String anotherString)

public int compareToIgnoreCase(String str)

public boolean equals(Object anObject)

public int compareToIgnoreCase(String str)


 字串的尋找與截取:定位,截取

public int indexOf(String str)

public int lastIndexOf(int ch)

public Stringsubstring(int beginIndex, int endIndex)

public Stringsubstring(int beginIndex) 

字串的大小寫轉換:

public StringtoLowerCase()

public StringtoUpperCase()

字串內容的替換:

public Stringreplace(CharSequence target, CharSequence replacement)

public StringreplaceAll(String regex, String replacement)

public StringreplaceFirst(String regex, String replacement)

【註:String regex Regex】 

分割字串:

public String[] split(String regex)

根據給定Regex的匹配拆分此字串,然後以數組的形式返回,注意傳回型別。

 

字串的其他動作:

public boolean contains(CharSequence s)

public boolean startsWith(String prefix)

public boolean endsWith(String suffix)

public Stringtrim()   返回字串的副本,忽略前置空白和尾部空白。

 等等等等,要學會使用API!!!注意這裡講到的都是String類本身的方法!

 

聯繫我們

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