zoukankan      html  css  js  c++  java
  • String类中replaceFirst、replaceAll、replace的代码区别(暂贴代码)

    此类选用的是jdk1.8.0_221版本的,在java.lang.String.java下的代码。


     String类中replaceFirst方法

     1     /**
     2      * Replaces the first substring of this string that matches the given <a
     3      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
     4      * given replacement.
     5      *
     6      * <p> An invocation of this method of the form
     7      * <i>str</i>{@code .replaceFirst(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
     8      * yields exactly the same result as the expression
     9      *
    10      * <blockquote>
    11      * <code>
    12      * {@link java.util.regex.Pattern}.{@link
    13      * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
    14      * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
    15      * java.util.regex.Matcher#replaceFirst replaceFirst}(<i>repl</i>)
    16      * </code>
    17      * </blockquote>
    18      *
    19      *<p>
    20      * Note that backslashes ({@code }) and dollar signs ({@code $}) in the
    21      * replacement string may cause the results to be different than if it were
    22      * being treated as a literal replacement string; see
    23      * {@link java.util.regex.Matcher#replaceFirst}.
    24      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
    25      * meaning of these characters, if desired.
    26      *
    27      * @param   regex
    28      *          the regular expression to which this string is to be matched
    29      * @param   replacement
    30      *          the string to be substituted for the first match
    31      *
    32      * @return  The resulting {@code String}
    33      *
    34      * @throws  PatternSyntaxException
    35      *          if the regular expression's syntax is invalid
    36      *
    37      * @see java.util.regex.Pattern
    38      *
    39      * @since 1.4
    40      * @spec JSR-51
    41      */
    42     public String replaceFirst(String regex, String replacement) {
    43         return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
    44     }

     String类中replaceAll方法

     1 /**
     2      * Replaces each substring of this string that matches the given <a
     3      * href="../util/regex/Pattern.html#sum">regular expression</a> with the
     4      * given replacement.
     5      *
     6      * <p> An invocation of this method of the form
     7      * <i>str</i>{@code .replaceAll(}<i>regex</i>{@code ,} <i>repl</i>{@code )}
     8      * yields exactly the same result as the expression
     9      *
    10      * <blockquote>
    11      * <code>
    12      * {@link java.util.regex.Pattern}.{@link
    13      * java.util.regex.Pattern#compile compile}(<i>regex</i>).{@link
    14      * java.util.regex.Pattern#matcher(java.lang.CharSequence) matcher}(<i>str</i>).{@link
    15      * java.util.regex.Matcher#replaceAll replaceAll}(<i>repl</i>)
    16      * </code>
    17      * </blockquote>
    18      *
    19      *<p>
    20      * Note that backslashes ({@code }) and dollar signs ({@code $}) in the
    21      * replacement string may cause the results to be different than if it were
    22      * being treated as a literal replacement string; see
    23      * {@link java.util.regex.Matcher#replaceAll Matcher.replaceAll}.
    24      * Use {@link java.util.regex.Matcher#quoteReplacement} to suppress the special
    25      * meaning of these characters, if desired.
    26      *
    27      * @param   regex
    28      *          the regular expression to which this string is to be matched
    29      * @param   replacement
    30      *          the string to be substituted for each match
    31      *
    32      * @return  The resulting {@code String}
    33      *
    34      * @throws  PatternSyntaxException
    35      *          if the regular expression's syntax is invalid
    36      *
    37      * @see java.util.regex.Pattern
    38      *
    39      * @since 1.4
    40      * @spec JSR-51
    41      */
    42     public String replaceAll(String regex, String replacement) {
    43         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
    44     }

     String类中replace方法

     1 /**
     2      * Replaces each substring of this string that matches the literal target
     3      * sequence with the specified literal replacement sequence. The
     4      * replacement proceeds from the beginning of the string to the end, for
     5      * example, replacing "aa" with "b" in the string "aaa" will result in
     6      * "ba" rather than "ab".
     7      *
     8      * @param  target The sequence of char values to be replaced
     9      * @param  replacement The replacement sequence of char values
    10      * @return  The resulting string
    11      * @since 1.5
    12      */
    13     public String replace(CharSequence target, CharSequence replacement) {
    14         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
    15                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
    16     }
  • 相关阅读:
    阿里云OSS对象存储 简单上传文件
    [转]eclipse查看某个java类属于哪个jar包
    win7自带照片查看器
    代码规范,李师兄的指导
    Python 结巴分词模块
    Python requests模块
    CTF
    Python requests模块在Windows下安装
    CentOS 6.5部署HTTP WEB服务器和FTP服务器
    Django搭建博客后台
  • 原文地址:https://www.cnblogs.com/LinQingYang/p/12683068.html
Copyright © 2011-2022 走看看