替换 Java 中的函数

Replace function in Java

如果我有这三个不同的字符串:

String line = "A man walking down the road";
String word = "the road";
String sub = "the street";

并且基本上想要 return 这个:

"A man walking down the street"

这可以用contains来完成(检查字符串'word'是否包含在字符串'text'中,然后replace来替换文本吗?因为我一直在尝试这个出去一段时间了,还没到地方

可以这样做:

String line = "A man walking down the road";
String word = "the road";
String sub = "the street";
System.out.println(line.replace(word,sub));