java常見類庫之String類詳解

常用構造方法
String()
初始化一個新創建的 String 對象,使其表示一個空字符序列。
String(byte[] bytes)
通過使用平臺的默認字符集解碼指定的 byte 數組,構造一個新的 String。
String(byte[] bytes, Charset charset)
通過使用指定的 charset 解碼指定的 byte 數組,構造一個新的 String。
String(char[] value)
分配一個新的 String,使其表示字符數組參數中當前包含的字符序列。

實例化:
1.String str=”sss”
2.String str=new String();

把其他格式變量轉換string如int,bool等一般用valueOf()函數。
下面給出String常用的方法。
方法:
char charAt(int index)
返回指定索引處的 char 值。
int compareTo(String anotherString)
按字典順序比較兩個字符串。
String concat(String str)
將指定字符串連接到此字符串的結尾。
boolean contains(CharSequence s)
當且僅當此字符串包含指定的 char 值序列時,返回 true。
boolean contentEquals(CharSequence cs)
將此字符串與指定的 CharSequence 比較。
boolean contentEquals(StringBuffer sb)
將此字符串與指定的 StringBuffer 比較。
boolean endsWith(String suffix)
測試此字符串是否以指定的後綴結束。
boolean equals(Object anObject)
將此字符串與指定的對象比較。
boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 比較,不考慮大小寫。
byte[] getBytes()
使用平臺的默認字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
byte[] getBytes(Charset charset)
使用給定的 charset 將此 String 編碼到 byte 序列,並將結果存儲到新的 byte 數組。
int indexOf(int ch)
返回指定字符在此字符串中第一次出現處的索引。
int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出現指定字符處的索引,從指定的索引開始搜索。
int indexOf(String str)
返回指定子字符串在此字符串中第一次出現處的索引。
int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出現處的索引,從指定的索引開始
int lastIndexOf(int ch)
返回指定字符在此字符串中最後一次出現處的索引。
int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最後一次出現處的索引,從指定的索引處開始進行反向搜索。
int lastIndexOf(String str)
返回指定子字符串在此字符串中最右邊出現處的索引。
int lastIndexOf(String str, int fromIndex)
返回指定子字符串在此字符串中最後一次出現處的索引,從指定的索引開始反向搜索。
int length()
返回此字符串的長度。
boolean matches(String regex)
告知此字符串是否匹配給定的正則表達式。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。
String replace(char oldChar, char newChar)
返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替換序列替換此字符串所有匹配字面值目標序列的子字符串。
String replaceAll(String regex, String replacement)
使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。
String replaceFirst(String regex, String replacement)
使用給定的 replacement 替換此字符串匹配給定的正則表達式的第一個子字符串。
String[] split(String regex)
根據給定正則表達式的匹配拆分此字符串。
String[] split(String regex, int limit)
根據匹配給定的正則表達式來拆分此字符串。
boolean startsWith(String prefix)
測試此字符串是否以指定的前綴開始。
boolean startsWith(String prefix, int toffset)
測試此字符串從指定索引開始的子字符串是否以指定前綴開始。

char[] toCharArray()
將此字符串轉換爲一個新的字符數組。
String toLowerCase()
使用默認語言環境的規則將此 String 中的所有字符都轉換爲小寫。
String toLowerCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字符都轉換爲小寫。
String toString()
返回此對象本身(它已經是一個字符串!)。
String toUpperCase()
使用默認語言環境的規則將此 String 中的所有字符都轉換爲大寫。
String toUpperCase(Locale locale)
使用給定 Locale 的規則將此 String 中的所有字符都轉換爲大寫。
static String valueOf(boolean b)
返回 boolean 參數的字符串表示形式。
static String valueOf(char c)
返回 char 參數的字符串表示形式。
static String valueOf(char[] data)
返回 char 數組參數的字符串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 數組參數的特定子數組的字符串表示形式。
static String valueOf(double d)
返回 double 參數的字符串表示形式。
static String valueOf(float f)
返回 float 參數的字符串表示形式。
static String valueOf(int i)
返回 int 參數的字符串表示形式。
static String valueOf(long l)
返回 long 參數的字符串表示形式。
static String valueOf(Object obj)
返回 Object 參數的字符串表示形式。

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