zoukankan      html  css  js  c++  java
  • 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了。


    希望有时间的同学提供性能方面的比較哦!谢谢!

  • 相关阅读:
    java基本类型和引用做形参传递
    新阶段新开始
    给网页中的button加动画效果
    数组对象常用的几个函数总结
    微信小程序使用页面栈改变上一页面的数据
    pc端和移动端的“窗口”(viewport)故事(part1)
    js的for循环中出现异步函数,回调引用的循环值总是最后一步的值?
    macOS Sierra 如何安装任何来源的软件
    Box-shadow制作漂亮的外阴影输入框
    button标签与input type=button标签使用的差异
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6690074.html
Copyright © 2011-2022 走看看