问题描述
使用String类的replaceFirst方法发现即使匹配还是"没有替换成功"
原调用
String oldString = "welcome to the real world, it sucks, you're gonna love it";
oldString.replaceFirst("real", "virtual");
System.out.println(oldString);
问题解析
replaceFirst() 方法并不会修改原String,而是返回替换后的新String
正确调用
String oldString = "welcome to the real world, it sucks, you're gonna love it";
String newString = oldString.replaceFirst("real", "virtual");
System.out.println(newString);