原地址連接:Scala 字符串 | 菜鳥教程
在平時的工作當中經(jīng)常會遇到一些字符串操作,但是string的方法有很多芙粱,也不是特別好記喂窟,剛好看到了一篇總結(jié)分享給大家。
下面列出了 java.lang.String 中常用的方法宠漩,我們可以在 Scala 中使用(別忘了引用java庫):
1,返回指定位置的字符
char charAt(int index)
2懊直,比較字符串與對象
int compareTo(Object o)
3扒吁,按字典順序比較兩個字符串
int compareTo(String anotherString)
4,按字典順序比較兩個字符串室囊,不考慮大小寫
int compareToIgnoreCase(String str)
5雕崩,將指定字符串連接到此字符串的結(jié)尾
String concat(String str)
6,將此字符串與指定的 StringBuffer 比較波俄。
boolean contentEquals(StringBuffer sb)
7晨逝,返回指定數(shù)組中表示該字符序列的 String
static String copyValueOf(char[] data)
8,返回指定數(shù)組中表示該字符序列的 String
static String copyValueOf(char[] data, int offset, int count)
9懦铺,測試此字符串是否以指定的后綴結(jié)束
boolean endsWith(String suffix)
10,將此字符串與指定的對象比較
boolean equals(Object anObject)
11支鸡,將此 String 與另一個 String 比較冬念,不考慮大小寫
boolean equalsIgnoreCase(String anotherString)
12,使用平臺的默認字符集將此 String 編碼為 byte 序列牧挣,并將結(jié)果存儲到一個新的 byte 數(shù)組中
byte getBytes()
13急前,使用指定的字符集將此 String 編碼為 byte 序列,并將結(jié)果存儲到一個新的 byte 數(shù)組中
byte[] getBytes(String charsetName
14瀑构,將字符從此字符串復制到目標字符數(shù)組
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
15裆针,返回此字符串的哈希碼
int hashCode()
16,返回指定字符在此字符串中第一次出現(xiàn)處的索引
int indexOf(int ch)
17寺晌,返回在此字符串中第一次出現(xiàn)指定字符處的索引世吨,從指定的索引開始搜索
int indexOf(int ch, int fromIndex)
18,返回指定子字符串在此字符串中第一次出現(xiàn)處的索引
int indexOf(String str)
19呻征,返回指定子字符串在此字符串中第一次出現(xiàn)處的索引耘婚,從指定的索引開始
int indexOf(String str, int fromIndex)
20,返回字符串對象的規(guī)范化表示形式
String intern()
21陆赋,返回指定字符在此字符串中最后一次出現(xiàn)處的索引
int lastIndexOf(int ch)
22沐祷,返回指定字符在此字符串中最后一次出現(xiàn)處的索引嚷闭,從指定的索引處開始進行反向搜索
int lastIndexOf(int ch, int fromIndex)
23,返回指定子字符串在此字符串中最右邊出現(xiàn)處的索引
int lastIndexOf(String str)
24赖临,返回指定子字符串在此字符串中最后一次出現(xiàn)處的索引胞锰,從指定的索引開始反向搜索
int lastIndexOf(String str, int fromIndex)
25,返回此字符串的長度
int length()
26兢榨,告知此字符串是否匹配給定的正則表達式
boolean matches(String regex)
27嗅榕,測試兩個字符串區(qū)域是否相等
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
28,測試兩個字符串區(qū)域是否相等
boolean regionMatches(int toffset, String other, int ooffset, int len)
29色乾,返回一個新的字符串誊册,它是通過用 newChar 替換此字符串中出現(xiàn)的所有 oldChar 得到的
String replace(char oldChar, char newChar)
30,使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串
String replaceAll(String regex, String replacement
31暖璧,使用給定的 replacement 替換此字符串匹配給定的正則表達式的第一個子字符串
String replaceFirst(String regex, String replacement)
32案怯,根據(jù)給定正則表達式的匹配拆分此字符串
String[] split(String regex)
33,根據(jù)匹配給定的正則表達式來拆分此字符串
String[] split(String regex, int limit)
34澎办,測試此字符串是否以指定的前綴開始
boolean startsWith(String prefix)
35嘲碱,測試此字符串從指定索引開始的子字符串是否以指定前綴開始。
boolean startsWith(String prefix, int toffset)
36局蚀,返回一個新的字符序列麦锯,它是此序列的一個子序列
CharSequence subSequence(int beginIndex, int endIndex)
37园匹,返回一個新的字符串钉鸯,它是此字符串的一個子字符串
String substring(int beginIndex)
38,返回一個新字符串音同,它是此字符串的一個子字符串
String substring(int beginIndex, int endIndex)
39千扶,將此字符串轉(zhuǎn)換為一個新的字符數(shù)組
char[] toCharArray()
40料祠,使用默認語言環(huán)境的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為小寫
String toLowerCase()
41,使用給定 Locale 的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為小寫
String toLowerCase(Locale locale)
42澎羞,返回此對象本身(它已經(jīng)是一個字符串K枵馈)
String toString()
43,使用默認語言環(huán)境的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為大寫
String toUpperCase()
44妆绞,使用給定 Locale 的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為大寫
String toUpperCase(Locale locale)
45顺呕,刪除指定字符串的首尾空白符
String trim()
46,返回指定類型參數(shù)的字符串表示形式
static String valueOf(primitive data type x)