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 ...
随机推荐
- Asp.Net MVC2.0 Url 路由入门---实例篇 【转】
本篇主要讲述Routing组件的作用,以及举几个实例来学习Asp.Net MVC2.0 Url路由技术. 接着上一篇开始讲,我们在Global.asax中注册一条路由后,我们的请求是怎么转到相应的Vi ...
- 文本框只能输入数字(兼容IE火狐)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 必须记住的 30 类 CSS 选择器
大概大家读知道`id`,`class`以及`descendant`选择器,并且整体都在使用它们,那么你正在错误拥有更大级别的灵活性的选择方式.这篇文章里面提到的大部分选择器都是在CSS3标准下的,所以 ...
- js遍历jstl数组
查询到在js中可以使用jstl <script> <c:forEach items="${channel.templates}" var="templa ...
- 用Telnet测试服务器的端口是否开通
可以用telnet测试远程服务器的端口是否开通,格式如下: telnet <server name> <port number> 例如: Telnet tserv 3389 ...
- Windows系统内存分析工具的介绍
Windows系统内存分析工具的介绍(进程管理器,资源管理器,性能监视器, VMMap, RamMap,PoolMon) 微软官方提供多种工具来分析Windows 的内存使用情况,除了系统自带的任 ...
- 迁移Windows下的MySQL时字符乱码问题
我们常常会直接复制一份MySQL的Data文件夹到新的环境下,正常情况下重新启动MySQL就可以使用.但有时也会遇到些问题: 1.程序訪问时提示找不到表,实际表已经存在 这样的情况是因为数据库全部者可 ...
- Python 使用pymongo操作mongodb库
Python 使用pymongo操作mongodb库 2016-12-31 21:55 1115人阅读 评论(0) 收藏 举报 分类: - - - Python(10) 版权声明:本文为博主原创文 ...
- maven 添加jar到中央/远程仓库
maven 添加jar到中央/远程仓库 开源中国 发表于 2014-08-23 00:08:00 commond: mvn deploy:deploy-file -DgroupId=com.tima. ...
- Asp.NET MVC 之 调试访问 webservice 时出现“ 无法找到资源 ”的错误
问题情景如标题,具体错误如下图: 出现以上情况,是程序将 .asmx 文件按控制器方式解析了,在 RouteConfig.cs 文件的 RegisterRoutes 方法中忽略 .asmx 文件,&q ...