Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace multiple words in a single string in Java?

Tags:

java

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 multiple words replaced and output in one single 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);      

    }
}
like image 216
shumaila Avatar asked Jul 19 '11 09:07

shumaila


People also ask

How do you replace multiple occurrences of a string in Java?

You can replace all occurrence of a single character, or a substring of a given String in Java using the replaceAll() method of java. lang. String class. This method also allows you to specify the target substring using the regular expression, which means you can use this to remove all white space from String.


3 Answers

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);
like image 183
dogbane Avatar answered Oct 26 '22 00:10

dogbane


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;
    }
}
like image 24
Martijn Courteaux Avatar answered Oct 25 '22 22:10

Martijn Courteaux


Now you can use StringUtils in commons-lang3 package.

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.7</version>
</dependency>

Code like below:

strOutput = StringUtils.replaceEach(inString, new String[]{"call me", "as soon as possible"}, new String[]{"cm", "asap"});
like image 36
jayxhj Avatar answered Oct 26 '22 00:10

jayxhj