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類本身的方法! |