Java replaceAll() 方法

Java 教程 · 2019-02-09 05:56:52

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(.*)", "codercto" ));
        System.out.print("匹配失败返回值 :" );
        System.out.println(Str.replaceAll("(.*)taobao(.*)", "codercto" ));
    }
}

以上程序执行结果为:

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

点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

Convergence Culture

Convergence Culture

Henry Jenkins / NYU Press / 2006-08-01 / USD 30.00

"Convergence Culture" maps a new territory: where old and new media intersect, where grassroots and corporate media collide, where the power of the media producer, and the power of the consumer intera......一起来看看 《Convergence Culture》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具