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 妈妈生日祝福语简短励志