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!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短