我有一个字符串,它是一个句子,我想't'用字符串"foo"和.'h'替换该字符的所有实例"bar"。
String sentence = "The tea is hot.";
我试图达到的最终结果:
"The fooea is fooobar."
这可能吗?
由于这听起来像家庭作业,我不会给出完整的解决方案。
但这里有一个非常非常强烈的提示:replaceAll()
使用replace:
sentence = sentence.replace("t", "foo").replace("h", "bar");
replaceAll此处不需要时采用正则表达式(因此效率不如replace)。
相关文件