【发布时间】:2012-12-07 22:20:51
【问题描述】:
我有一个字符串就是一个句子,我想用字符串 "foo" 和 'h' 替换字符 't' 的所有实例"bar"。
String sentence = "The tea is hot.";
我想要达到的最终结果:
"The fooea is fooobar."
这可能吗?
【问题讨论】:
-
是的,这是可能的。你试过什么?你的最终结果不应该是-
"The fooea is barofoo."吗? -
你需要:-
String#replace方法。 -
@RohitJain 实际上应该是:Tbare fooea is barofoo。
-
不,应该是“Tbare fooea is barofoo”。
-
@CodeAddict。不,OP 不会替换
T,只会替换t。