Java 中 String 的常用方法(二)
本文介紹剩下的一些常用的 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 google5、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, google6、valueOf 方法
將其他類型的對象轉換為字符串,包括了 boolean,char,char[],double,float,int,long 以及 Object 類型。
返回對應字符串或者 null。
轉載于:https://www.cnblogs.com/JavaSubin/p/5492173.html
總結
以上是生活随笔為你收集整理的Java 中 String 的常用方法(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高动态范围成像
- 下一篇: Javascript之全局变量和局部变量