先澄清几个误区
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.5String 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是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也是字符串); 2)...
相信会java的同学估计都用过replace、replaceAll、replaceFirst这三个函数,可是,我们真的懂他们吗?下面通过这篇文章大家再来好好学习学习下这几个函数。
主要介绍了Java中replace与replaceAll区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了java中replace()和replaceAll()的区别,两者都是常用的替换字符的方法,感兴趣的小伙伴们可以参考一下
介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下
JavaScript 中使用 replace 达到 replaceAll的效果,其实就用利用的正则的全局替换。
将一段文字中的字符串全部替换 PowerBuilder ReplaceAll
JS 没有提供replaceAll这样的方法。使用正则表可以达成Replace 的效果,感兴趣的朋友看看下面的示例
js里没有replaceAll的函数,需要我们自定义 代码如下: String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); } 2、使用js的replace进行处理 代码如下: str=str....
Console.WriteLine(ms.ReplaceLast("hello world","l","*")); //输出”hello wor*d" Console.WriteLine(ms.ReplaceAll("hello world","l","*")); //输出”he**o wor*d" ArrayList list=ms.GetPosList("hello world",...
string.prototype.replaceall 用于String.prototype.replaceAll的ES Proposal规范填充程序。 如果不可用或不String.prototype....// replaceAll and replace are the same, when given a global regex to replace as
而str.replace(/\-/g,”!”)则可以全部替换掉匹配的字符(g为全局标志)。 replace() The replace() method returns the string that results when you replace text matching its first argument (a regular ...
replaceAll()方法的作用和replaceWith()方法是完全一样的。 语法结构: 代码如下:$(content).replaceAll(selector) 参数列表: 参数 描述 content 用于替换的内容。 selector 用于查找所要被替换的元素 ...
第一次发现JavaScript中replace() 方法如果直接用str.replace(“-“,”!”) 只会替换第一个匹配的字符. 而str.replace(/\-/g,”!”)则可以全部替换掉匹配的字符(g为全局标志)。 replace() The replace() method ...
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? “gi”: “g...