Java replaceFirst() 方法
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 sunny,I am from itmind。");
System.out.print("返回值 :" );
System.out.println(Str.replaceFirst("sunny", "google" ));
System.out.print("返回值 :" );
System.out.println(Str.replaceFirst("(.*)sunny(.*)", "google" ));
}
}
以上程序执行结果为:
返回值 :hello google,I am from sunny。
返回值 :sunny
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/97653.html