在 java 中将 String 替换为另一个字符串

2022-08-31 10:04:16

什么函数可以用另一个字符串替换一个字符串?

示例 #1:什么将替换为 ?"HelloBrother""Brother"

示例#2:什么将替换为?"JAVAISBEST""BEST"


答案 1

替换方法是您要查找的。

例如:

String replacedString = someString.replace("HelloBrother", "Brother");

答案 2

试试这个:https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29

String a = "HelloBrother How are you!";
String r = a.replace("HelloBrother","Brother");

System.out.println(r);

这将打印出“兄弟你好吗!