How to replace multiple words on one line in Java?

I am writing a program that will replace several words in one line. I use this code, but it replaces the word, but gives the result in two different lines. I want several words to be replaced and displayed on the same line.

import java.util.*; public class ReplaceString { public static void main(String[] args) { new ReplaceString().run(); } public void run() { System.out.println("Input String:\n");//// Scanner keyboardScanner = new Scanner(System.in);///// String inString = keyboardScanner.nextLine();///// String strOutput = inString.replace("call me","cm"); System.out.println(strOutput); String strOutput1 = inString.replace("as soon as possible","asap"); System.out.println(strOutput1); } } 
+6
source share
8 answers

If you want to do this in one 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 instance:

 //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); 
+12
source

Of course, it prints two lines: you have two print statements. Use this code:

 import java.util.*; public class ReplaceString { public static void main(String[] args) { new ReplaceString().run(); } public void run() { System.out.println("Input String:\n");//// Scanner keyboardScanner = new Scanner(System.in);///// String inString = keyboardScanner.nextLine();///// String shortMessage = shortifyMessage(inString); System.out.println(shortMessage); } public String shortifyMessage(String str) { String s = str; s = s.replace("call me", "cm"); s = s.replace("as soon as possible", "asap"); // Add here some other replacements return s; } } 
+4
source

Use System.out.print() instead of System.out.println()

0
source
  String strOutput1 = inString.replace("as soon as possible","asap"); 

You have to change this to

  String strOutput1 = strOutput .replace("as soon as possible","asap"); 
0
source

Use MessageFormat.format (queryString, retrieveArguments (). ToArray ());

0
source

The root problem is that after the first replacement you will not be able to work with the same original string again. I think that the right way to do this would be to use different copies and switch from one content to another while replacing it. It might be better than this solution to just make an additional replacement after each replacement to remove templates that have already been replaced .;)

0
source

Instead of using replace use replaceAll , which worked for me

 String strOutput = inString.replaceAll("call me","cm").replaceAll("as soon as possible","asap"); 
0
source

All of the above answers may be correct. However, replacing each row once is inefficient. The following code from Apache Commons StringUtils will help it efficiently replace all strings in one go.

  System.out.println("Input String:\n");//// Scanner keyboardScanner = new Scanner(System.in);///// String inString = keyboardScanner.nextLine();///// StringUtils.replaceEach(inString, new String[]{"call me", "as soon as possible"}, new String[]{"cm", "asap"}); 

Please note that: the above method does not work when replacing words in the previous replacement result. For instance:

 StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) 

will give the result: "dcte"

0
source

Source: https://habr.com/ru/post/893056/


All Articles