How to replace a specific string in Java?
You can use the replace() method in Java to replace a specified string. This method takes two parameters, the first one being the string to be replaced and the second one being the new string used for replacement.
“做任何事情都要先考虑后果。”
“It is important to consider the consequences before doing anything.”
String str = "Hello World!";
String newStr = str.replace("World", "Java");
System.out.println(newStr);
The output is:
Hello Java!