先澄清几个误区

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的差别的更多相关文章

  1. java replace和replaceAll

    replace和replaceAll是JAVA中常用的替换字符的方法 public String replace(char oldChar, char newChar)         在字符串中用n ...

  2. replace和replaceAll的区别

      replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Cha ...

  3. java中replace和replaceAll的区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharS ...

  4. JAVA中REPLACE和REPLACEALL的区别(转)

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:  1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Char ...

  5. java中replace()和replaceAll()区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是: 1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharS ...

  6. java基础---->String中replace和replaceAll方法

    这里面我们分析一下replace与replaceAll方法的差异以及原理. replace各个方法的定义 一.replaceFirst方法 public String replaceFirst(Str ...

  7. js replace 与replaceall实例用法详解

    这篇文章介绍了js replace 与replaceall实例用法详解,有需要的朋友可以参考一下stringObj.replace(rgExp, replaceText) 参数 stringObj 必 ...

  8. JAVA中替换字符的方法replace和replaceAll 区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSe ...

  9. Java中String的替换函数:replace与replaceAll的区别

    例如有如下x的字符串  String x = "[kllkklk\\kk\\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相同的结果  r ...

随机推荐

  1. 远程连接Oracle数据库

    ylbtech-Oracle:远程连接Oracle数据库  所谓远程连接Oracle数据库,是指Oracle数据库服务器和Oracle客户端分别安装在2台电脑上,我们使用Oracle客户端来连接在另一 ...

  2. 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.cpp -- 模型测试

    测试代码.能看到,训练的时候是单个样本.单个样本的训练的,在NN中是属于“stochastic gradient descent”,否则,一批样本在一起的,就是“standard gradient d ...

  3. BERT深度解析

    这篇文章看起来很不错: https://blog.csdn.net/qq_39521554/article/details/83062188 仔细看看. 也可以看这个github,一样的文章: htt ...

  4. Tensorflow 模型持久化saver及加载图结构

    主要内容: 1. 直接保存,加载模型; (可以指定加载,保存的var_list) 2. 加载,保存指定变量的模型 3. slim加载模型使用 4. 加载模型图结构和参数等 tensorflow 恢复部 ...

  5. Http协议中Get和Post的浅谈

    起名困难户,每次写文章最愁的就是不知道该如何起个稍具内涵的名字,如果这篇文章我只是写写Get和Post的区别,我可以起个名字“Get和Post的那点事”,如果打算阐述一下Http协议原理性内容,那该叫 ...

  6. Centos7 搭建lnmp环境 (centos7+nginx+MySQL5.7.9+PHP7)

    阿里云一台服务器出现问题! 我估计是一键安装包环境的原因,所以打算重新搭建下环境! 首先,当然是先做好快照!安全第一! 对系统盘做更换系统操作,装上纯净版的centos. 装好后,进入系统 一.挂载数 ...

  7. Druid连接池简介和配置

    Druid是什么?有什么作用?  Druid首先是一个数据库连接池,但它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. Druid ...

  8. js面向对象之继承-原型继承

    //animal 父类 超类 var Animal = function(name) { this.name = name; this.sayhello = function() { alert(&q ...

  9. 以快板之名说Android 应用程序电源管理

    当里个当,当里个当.Android开发UE(用户体验)为导向,首要任务便是省电量. 当里个当,当里个当.有一设备立足于墙边,这个设备唤固定电话.你的app造成这样,用户很快把你弃墙角.你咆哮耗电奈何与 ...

  10. 大数据开发实战:Stream SQL实时开发二

    1.介绍 本节主要利用Stream SQL进行实时开发实战,回顾Beam的API和Hadoop MapReduce的API,会发现Google将实际业务对数据的各种操作进行了抽象,多变的数据需求抽象为 ...