Java程序仅用新字符串替换给定String的第一次出现
使用此replaceFirst()方法仅用新出现的给定String替换第一次出现的String。
假设我们有以下字符串。
String str = "这是演示文字!";
我们必须用“EV”替换首次出现的“IS”。为此,使用replaceFirst()方法。
str.replaceFirst("IS", "EV");以下是最终示例,其中替换了首次出现的“IS”。
示例
public class Demo {
public static void main(String[] args) {
String str = "这是演示文字!";
System.out.println("String = "+str);
System.out.println("Replacing only the first occurrence of substring IS...");
System.out.println("Updated string = "+str.replaceFirst("IS", "EV"));
}
}输出结果
String = 这是演示文字! Replacing only the first occurrence of substring IS... Updated string = THEV IS DEMO TEXT!
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短