Java-String的replaceAll()方法(力扣20题相关)

https://www.runoob.com/java/java-string-replaceall.html

原地址在上面

replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。

语法

public String replaceAll(String regex, String replacement)

参数

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

  • newChar -- 用来替换每个匹配项的字符串。

返回值

成功则返回替换的字符串,失败则返回原始字符串。

实例

public class Test {     public static void main(String args[]) {         String Str = new String("www.google.com");         System.out.print("匹配成功返回值 :" );         System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));         System.out.print("匹配失败返回值 :" );         System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));     } }

以上程序执行结果为:

匹配成功返回值 :runoob
匹配失败返回值 :www.google.com

猜你喜欢

转载自www.cnblogs.com/lzh1043060917/p/12791689.html