Java 中 String 的常用方法(二)

shaopiing發表於2016-05-14

本文介紹剩下的一些常用的 String 中的方法。

1、replace 方法 、replaceFirst 方法和 replaceAll 方法

replace(char oldChar, char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.

replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.

這幾個方法都是用指定的字元或字串將原有的字串中的對應內容進行替換,前面兩個不支援正規表示式,後邊兩個支援正則

String x = "[hello\\google\\bye]";
System.out.println(x.replace('\\', '/'));
System.out.println(x.replace("oo", "++"));
System.out.println(x.replace("\\", "++"));
System.out.println(x.replaceFirst("\\\\", "/"));
System.out.println(x.replaceAll("oo", "++"));
System.out.println(x.replaceAll("\\\\", "++"));

輸出結果為:

[hello/google/bye]
[hello\g++gle\bye]
[hello++google++bye]
[hello/google\bye]
[hello\g++gle\bye]
[hello++google++bye]

根據測試 replaceAll 函式要更快一些。看原始碼發現,replace 函式裡面仍使用 replaceAll 函式。

總體原則:當字串無法確定是否具有轉義字元時,而且也不需要轉義時,建議使用 replace 函式;否則,使用 replaceAll 函式。

PS:正規表示式中匹配一個反斜槓為何需要用四個反斜槓?

分析一下字串 "\\\\",第一個和第三個為轉義符,第二個和第四個為斜槓本身。

字串中表示斜槓就需要兩個斜槓 "\\",

正規表示式裡邊的斜槓則需要轉義,用 "\\" 表示,

所以我們先要表示正規表示式裡邊的斜槓 "\\",然後在用字串表示出來,而這 2 個斜槓分別需要一個轉義符,這樣就成了 4 個斜槓在正規表示式中表示一個斜槓。

2、matches 方法

matches(String regex)
Tells whether or not this string matches the given regular expression.

該方法用來判斷這個字串是否匹配給定的正規表示式,符合則返回 true,反之則返回 false。

3、split 方法

split(String regex)
Splits this string around matches of the given regular expression.

split(String regex, int limit)
Splits this string around matches of the given regular expression.

字串分割的方法,返回值為一個 String 型別的陣列

第一個引數 int limit 可以限制進行正則匹配的次數,

如果 limit > 0,則進行正則匹配的次數為 limit - 1 次,

如果 limit < 0,則不限制匹配的次數,

如果 limit = 0,則不限制匹配的次數,並且分隔之後的陣列結尾的空字串被忽略。

String str = "boo:and:foo";
System.out.println(Arrays.toString(str.split(":", 2)));
System.out.println(Arrays.toString(str.split(":", 5)));
System.out.println(Arrays.toString(str.split(":", -2)));
System.out.println(Arrays.toString(str.split("o", 5)));
System.out.println(Arrays.toString(str.split("o", -2)));
System.out.println(Arrays.toString(str.split("o", 0)));

輸出結果為:

[boo, and:foo]
[boo, and, foo]
[boo, and, foo]
[b, , :and:f, , ]
[b, , :and:f, , ]
[b, , :and:f]

4、toLowerCase 方法和 toUpperCase 方法

toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.

toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.

大小寫轉換方法

String str = "hello google";
System.out.println(str.toUpperCase());
String str1 = "HELLO google";
System.out.println(str1.toLowerCase());

輸出結果為:

HELLO GOOGLE
hello google

5、trim 方法

trim()
Returns a string whose value is this string, with any leading and trailing whitespace removed.

去掉給定字串前後的空白

String str = "       hello, google              ";
System.out.println(str.trim());

輸出結果:

hello, google

6、valueOf 方法

將其他型別的物件轉換為字串,包括了 boolean,char,char[],double,float,int,long 以及 Object 型別。

返回對應字串或者 null。

相關文章