读书人

String 种中replace 和replaceAll方法

发布时间: 2012-09-15 19:09:28 作者: rapoo

String 类中replace 和replaceAll方法的区别

做项目的时候有发现问题记录下的习惯,今天刚了解到点东西,发发牢骚而已

?

做解析域控制器(Active Directory)的用户和组织单位的时候需要对特殊符号的处理,

?

习惯性的 str.replaceAll("+","\\+");

?

结果出现java.util.regex.PatternSyntaxException

?

查询了下原因是要改变的字符序列中包含正则表达式中特殊的字符,用replace 就解决了。

?

那么replace 和replaceAll究竟有什么区别呢

?

帮助文档

replace:使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

replaceAll:使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

?

玩玩源码

public String replaceAll(String regex, String replacement) {return Pattern.compile(regex).matcher(this).replaceAll(replacement);    }

?

public String replace(CharSequence target, CharSequence replacement) {        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));    }

?

不同之处

1.Pattern.LITERAL :

 /**     * Enables literal parsing of the pattern.     *     * <p> When this flag is specified then the input string that specifies     * the pattern is treated as a sequence of literal characters.     * Metacharacters or escape sequences in the input sequence will be     * given no special meaning.     *     * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on     * matching when used in conjunction with this flag. The other flags     * become superfluous.     *     * <p> There is no embedded flag character for enabling literal parsing.     * @since 1.5     */    public static final int LITERAL = 0x10;
?

?? 在来个中文的

?

public static final int LITERAL
启用模式的字面值解析。

指定此标志后,指定模式的输入字符串就会作为字面值字符序列来对待。输入序列中的元字符或转义序列不具有任何特殊意义。

?

2.Matcher.quoteRelacement()

/**     * Returns a literal replacement <code>String</code> for the specified     * <code>String</code>.     *     * This method produces a <code>String</code> that will work     * as a literal replacement <code>s</code> in the     * <code>appendReplacement</code> method of the {@link Matcher} class.     * The <code>String</code> produced will match the sequence of characters     * in <code>s</code> treated as a literal sequence. Slashes ('\') and     * dollar signs ('$') will be given no special meaning.     *     * @param  s The string to be literalized     * @return  A literal string replacement     * @since 1.5     */    public static String quoteReplacement(String s) {        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))            return s;        StringBuffer sb = new StringBuffer();        for (int i=0; i<s.length(); i++) {            char c = s.charAt(i);            if (c == '\\') {                sb.append('\\'); sb.append('\\');            } else if (c == '$') {                sb.append('\\'); sb.append('$');            } else {                sb.append(c);            }        }        return sb.toString();    }

??? 中文解析

?

public static String quoteReplacement (String s)
返回指定 String 的字面值替换 String。 此方法将生成一个 String,它将用作 Matcher 类的 appendReplacement 方法中的字面值替换 s。所产生的 String 将与作为字面值序列的 s 中的字符序列匹配。斜线 ('\') 和美元符号 ('$') 将不具有任何特殊意义。

看到这里了我懂了, 你也懂的。

读书人网 >软件架构设计

热点推荐