Java - String replaceAll() 方法



Java String replaceAll() 方法用于替换 String 对象中所有特定模式的出现。

此方法接受正则表达式和替换字符串作为参数,搜索当前字符串以查找给定的正则表达式,并将匹配的子字符串替换为给定的替换字符串。

replaceAll() 方法和 replace() 方法之间的唯一区别在于 replaceAll() 方法替换模式(使用正则表达式)。而 replace() 方法替换字符串。

注意:请注意,如果替换字符串包含美元符号 ($) 或反斜杠 (\),则结果可能与将其作为文字替换字符串处理的结果不同。

语法

以下是Java String replaceAll()方法的语法:

public String replaceAll(String regex, String replacement)

参数

  • regex - 这是要与此字符串匹配的正则表达式。

  • replacement - 这是要替换每个匹配项的字符串。

返回值

此方法返回结果字符串。

从字符串示例中替换元字符

以下示例显示了通过替换正则表达式中给出的元字符来使用 Java String replaceAll() 方法:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "!!Tutorials!!Point", str2;
      String substr = "**", regex = "!!";    
      
      // prints string1
      System.out.println("String = " + str1);    
      
      /* replaces each substring of this string that matches the given
      regular expression with the given replacement */
      str2 = str1.replaceAll(regex, substr);    
      System.out.println("After Replacing = " + str2);
   }
}

输出

如果编译并运行以上程序,它将产生以下结果:

String = !!Tutorials!!Point
After Replacing = **Tutorials**Point

从字符串示例中替换单词的出现

在以下示例中,使用 replaceAll() 方法替换单词的所有出现:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String str = "Next time there won't be a next time after a certain time.";
      System.out.println("The given string is: " + str);
      
      // replacing all occurrences of "time" to "day"
      String newString = str.replaceAll("time", "day");
      System.out.println("The new replaced string is: " + newString);
   }
}

输出

如果编译并运行以上程序,输出将显示如下:

The given string is: Next time there won't be a next time after a certain time.
The new replaced string is: Next day there won't be a next day after a certain day.

从字符串示例中替换所有空格字符

现在,我们将使用 replaceAll() 方法删除给定字符串中所有空格的出现:

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String str = "Next time there won't be a next time after a certain time.";
      System.out.println("The given string is: " + str);
      
      // replacing all occurrences of "time" to "day"
      String newString = str.replaceAll("\\s", "");
      System.out.println("The new replaced string is: " + newString);
   }
}

输出

执行以上程序后,输出如下:

The given string is: Next time there won't be a next time after a certain time.
The new replaced string is: Nexttimetherewon'tbeanexttimeafteracertaintime.

检查无效模式示例的 PatternSyntaxException

无效的正则表达式会导致 replaceAll() 方法抛出 PatternSyntaxException。请查看以下示例:

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "Welcome to tutorials point.";
      
      // invalid regular expression provided.
      String regExpression = "\\";
      s = s.replaceAll(regExpression, "point ");
      System.out.println("The replaced string is: " + s); 
   }
}

PatternSyntaxException

以上程序的输出如下:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
      at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1799)
      at java.base/java.util.regex.Pattern.(Pattern.java:1440)
      at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
      at java.base/java.lang.String.replaceAll(String.java:2938)
      at StringDemo.main(StringDemo.java:7)
java_lang_string.htm
广告

© . All rights reserved.