日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 中 String 的常用方法(二)

發布時間:2023/12/13 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 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。

轉載于:https://www.cnblogs.com/JavaSubin/p/5492173.html

總結

以上是生活随笔為你收集整理的Java 中 String 的常用方法(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。