replace multiple words in a string in java
Code snippet on how to replace multiple words in a string in java
String originalString = "Here is a string with words that need to be replaced";
String replacedString = originalString.replaceAll("words","characters");
System.out.println(replacedString);
This code declares a variable originalString
and assigns it a string value. It then uses the replaceAll
method of the String
class to replace the word “words” with “characters” in the originalString
variable and assigns the new string to the variable replacedString
. Finally, it prints out the value of the replacedString
variable using the println
method of the System
class.