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的差别的更多相关文章
- java replace和replaceAll
replace和replaceAll是JAVA中常用的替换字符的方法 public String replace(char oldChar, char newChar) 在字符串中用n ...
- replace和replaceAll的区别
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Cha ...
- java中replace和replaceAll的区别
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharS ...
- JAVA中REPLACE和REPLACEALL的区别(转)
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Char ...
- java中replace()和replaceAll()区别
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharS ...
- java基础---->String中replace和replaceAll方法
这里面我们分析一下replace与replaceAll方法的差异以及原理. replace各个方法的定义 一.replaceFirst方法 public String replaceFirst(Str ...
- js replace 与replaceall实例用法详解
这篇文章介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下stringObj.replace(rgExp, replaceText) 参数 stringObj 必 ...
- JAVA中替换字符的方法replace和replaceAll 区别
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSe ...
- Java中String的替换函数:replace与replaceAll的区别
例如有如下x的字符串 String x = "[kllkklk\\kk\\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相同的结果 r ...
随机推荐
- 数学图形(1.48)Cranioid curve头颅线
这是一种形似乎头颅的曲线.这种曲线让我想起读研的时候,搞的医学图像三维可视化.那时的原始数据为脑部CT图像.而三维重建中有一种方式是面绘制,是将每一幅CT的颅骨轮廓提取出来,然后一层层地罗列在一起,生 ...
- Construct Binary Tree from Preorder and Inorder Traversal leetcode java
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- JavaScript类的写法
js类的基本含义 我们知道,在js中,是没有类的概念的.类的所有实例对象都从同一个原型对象上继承属性,因此,原型对象是类的核心. 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是 ...
- Linux系统下对NFS服务安全加固的方法
NFS(Network File System)是 FreeBSD 支持的一种文件系统,它允许网络中的计算机之间通过 TCP/IP 网络共享资源.不正确的配置和使用 NFS,会带来安全问题. 概述 N ...
- junit与spring-data-redis 版本对应成功的
spring-data-redis 版本:1.7.2.RELEASE junit 版本:4.12
- 如何用简单例子讲解 Q - learning 的具体过程?
作者:牛阿链接:https://www.zhihu.com/question/26408259/answer/123230350来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 阿里云centos 6安装iRedmail过程
全新系统 yum update cd /root wget http://www.iredmail.com/iRedMail-0.8.7.tar.bz2 tar xvf iRedMail-0.8.7. ...
- Win10远程桌面出现身份验证错误要求的函数不受支持
Win10更新了,远程连不上了,还是手动来修改,用户体验不好,差评! 解决方法: 在本地(而非远程机),运行 gpedit.msc,打开本地组策略:计算机配置>管理模板>系统>凭据分 ...
- CAD块参照转实体
经常,需要在CAD中插入块,比如图框,它的类型是INSERT,而不是Line和PolyLine.一般情况下,我们是不会去编辑它的,但有的时候需要选择它,比如在选择打印范围时,默认为过滤掉INSERT类 ...
- 微信小程序 - async/await
下面只是做一些介绍以及使用的原因,详情介绍还请移步博主:https://www.cnblogs.com/SamWeb/p/8417940.html regenerator-runtime下载:http ...