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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Replace和ReplaceAll的差别

發布時間:2023/12/29 综合教程 31 生活家
生活随笔 收集整理的這篇文章主要介紹了 Replace和ReplaceAll的差别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先澄清幾個誤區

1、CharSequence 不是 Char :有些小朋友依據參數的類型選擇Replace或ReplaceAll方法

2、Replace 和 ReplaceAll :并非有些小朋友想象的Replace僅僅替代一個出現的字符,ReplaceAll 替換全部字符

3、循環替換的誤區

		String eventJson = ".............";
		Iterator<Entry<String, String>> itPro = map.entrySet().iterator();
		while (itPro.hasNext()) {
			Entry<String, String> entry = itPro.next();
			eventJson.replace(entry.getKey(), entry.getValue());
		}
		System.out.println(eventJson);

上面偽代碼并不能得到你想要的結果。

eventJson.replace(entry.getKey(), entry.getValue());
須要替換成
eventJson = eventJson.replace(entry.getKey(), entry.getValue());

不耐煩的同學如今一定急著想知道兩者的差別呢,如今開始解說:

你能夠去看看兩個方法的定義:

String java.lang.String.replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example,
replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
Parameters:
target The sequence of char values to be replaced
replacement The replacement sequence of char values
Returns:
The resulting string
Throws:
NullPointerException - iftarget orreplacement is null.
Since:
1.5

String java.lang.String.replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as
the expression java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string;
see Matcher.replaceAll. Use java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.Parameters:regex the regular expression to which this string is to be matchedreplacement the string to be substituted for each
matchReturns:The resulting StringThrows:PatternSyntaxException - if the regular expression's syntax is invalidSince:1.4See Also:java.util.regex.Pattern@specJSR-51

一目了然!

String.Replace 主要是針對字符串的替換。

String.ReplaceAll 主要是用正則表達式的子字符串進行替換。

我們做個測試看看!

		System.out.println("1234567890abcdef".replace("12345", "ABCDE"));
		System.out.println("1234567890abcdef".replaceAll("12345", "ABCDE"));
		System.out.println("!@#$%^&*()-=Abcd".replace("#$%^&", "OK"));
		System.out.println("!@#$%^&*()-=Abcd".replaceAll("#$%^&", "OK"));

運行結果!

ABCDE67890abcdef
ABCDE67890abcdef
!@OK*()-=Abcd
!@#$%^&*()-=Abcd

明顯最后一行替換失敗了,由于有正則表達式字符。

追求性能的同學一定會問這兩個方法誰快,這個就留個好奇的你了,呵呵...

這邊沒時間做大量的測試給你求證了,可是給出不嚴謹的個人猜想例如以下:

Replace比ReplaceAll性能略好。

可是有正則匹配的時候你肯定選用ReplaceAll了。

希望有時間的同學提供性能方面的比較哦!謝謝。

總結

以上是生活随笔為你收集整理的Replace和ReplaceAll的差别的全部內容,希望文章能夠幫你解決所遇到的問題。

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