Java replaceFirst() 方法

Java 教程 · 2019-02-09 06:12:41

replaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。

语法

public String replaceFirst(String regex,
                           String replacement)

参数

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

  • replacement -- 用来替换第一个匹配项的字符串。

返回值

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

实例

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello codercto,I am from codercto。");

        System.out.print("返回值 :" );
        System.out.println(Str.replaceFirst("codercto", "google" ));
        System.out.print("返回值 :" );
        System.out.println(Str.replaceFirst("(.*)codercto(.*)", "google" ));
    }
}

以上程序执行结果为:

返回值 :hello google,I am from codercto。
返回值 :google

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

查看所有标签

Charlotte's Web

Charlotte's Web

E. B. White / Puffin Classics / 2010-6-3 / GBP 6.99

This is the story of a little girl named Fern who loved a little pig named Wilbur and of Wilbur's dear friend, Charlotte A. Cavatica, a beautiful large grey spider. With the unlikely help of Templeton......一起来看看 《Charlotte's Web》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试