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 红包祝福语简短10字
18 周六早晨祝福语简短