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 祝福语驱散霉运的话简短