I\'m writing a program that will replace multiple words in a single string. I\'m using this code but it is replacing word but giving result in two different lines. I want
If you want to do it in a single statement you can use:
String strOutput = inString.replace("call me","cm").replace("as soon as possible","asap");
Alternatively, if you have many such replacements, it might be wiser to store them in some kind of data structure such as a 2d-array. For example:
//array to hold replacements
String[][] replacements = {{"call me", "cm"},
{"as soon as possible", "asap"}};
//loop over the array and replace
String strOutput = inString;
for(String[] replacement: replacements) {
strOutput = strOutput.replace(replacement[0], replacement[1]);
}
System.out.println(strOutput);