length()
-
charAt()
:獲取對應索引上的char
-
getChars()
、getBytes()
:復制char或byte
-
toCharArray()
:生成一個char[]
-
toUpperCase()
、toLowerCase()
-
equals()
、contentEquals()
、equalsIgnoreCase()
-
regionMatcher()
:比較兩個字符串某段區域是否相等
-
compareTo()
:返回-1,0,1
contains()
-
startsWith()
、endsWith()
:檢查前綴和后綴。
-
indexOf()
、lastIndexOf()
:向前或向后獲取對應字符或字符串的索引。
-
substring()
、subSequence()
concat()
-
replace()
:將指定字符串替換為另一個字符串。
-
replaceFirst()
、replaceAll()
:替換正則表達式匹配的子串。
-
trim()
:刪除兩端空白字符。
-
valueOf()
:用于把整數轉換為字符,或整數組轉換為字符組。
-
format()
:根據格式化產生一個字符串。
-
match()
:檢查字符串是否符合某種正則表達式模式。
-
split()
:將字符串從正則表達式匹配的地方切開。
-
intern()
:如果配合new String().intern()使用,若內存池中已經包含要new的字符串列,則返回此String引用,而不創建。
簡介
- 使用
StringBuilder
比直接操作String要高效。
相關方法
append()
toString()
delete()
insert()
repleace()
substring()
reverse()
正則表達式
規則
簡單示例
代碼
String reg = "[a-z]";
String str = "abcZde";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while (m.find()) {
System.out.print("Match\"" + m.group() + "\" at positions "
+ m.start() + "-" + (m.end() - 1) + "\n");
}
結果
Match"a" at positions 0-0
Match"b" at positions 1-1
Match"c" at positions 2-2
Match"d" at positions 4-4
Match"e" at positions 5-5
-
matches()
:檢查Matcher對象中是否整個字符串都能匹配正則表達式,返回布爾值。(會影響下次搜索位置)
-
lookingAt()
:檢查Matcher對象是否從一開始就有匹配,返回布爾值。(會影響下次搜索位置)
-
find()
:檢查Matcher對象中是否還有匹配。(會影響下次搜索位置)
-
reset()
:重設要進行搜索的字符串。
- 組:組是用括號劃分的正則表達式。如正則表達式A(B(C))D中,組0是ABCD,組1是BC,組2是C。
-
group()
:返回上次匹配操作(如find)對應的字符串。
-
group(i)
:返回上次匹配操作(如find)對應的組號的字符串。
-
groupCount()
:返回上次匹配操作(如find)分組數目。
-
start(i)
:返回上次匹配操作(如find)對應的組號的字符串在原字符串中的起始索引。
-
end(i)
:返回上次匹配操作(如find)對應的組號的字符串在原字符串中的終止索引。
-
appendReplacement()
:String類的replaceFirst和replaceAll來說,因為無法引用到匹配的字符串,因此不能對匹配做處理后再替換。appendReplacement則可以。
-
appendTail()
:配合appendReplacement使用
String s = "aeiou AEIOU";
StringBuffer sbuf = new StringBuffer();
Pattern p = Pattern.compile("[aeiou]");
Matcher m = p.matcher(s);
while (m.find()) {
m.appendReplacement(sbuf, m.group().toUpperCase());
}
m.appendTail(sbuf);
System.out.print(sbuf + "\n");
-
replaceFirst()
、replaceAll()
-
compile()
:第一參數是正則表達式,第二參數可以是如下標記:
-
split()
:第一參數是分割的字符串,第二參是要分割幾次。
-
matcher()
:傳入要應用正則表達式的字符串,返回Matcher對象。
- 構造器:用File、InputStream、String構造。
hasNext()
next()
nextLine()
nextInt()
nextDouble()
-
useDelimiter()
:接受一個正則表達式,匹配掃描時要跳過的字符串。
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。