【问题标题】:How to replace multiple words in a single string in Java?如何在Java中替换单个字符串中的多个单词?
【发布时间】:2011-10-08 09:04:53
【问题描述】:

我正在编写一个程序,它将替换单个字符串中的多个单词。我正在使用此代码,但它正在替换 word 但在两个不同的行中给出结果。我想在一行中替换多个单词并输出。

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);      

    }
}

【问题讨论】:

标签: java


【解决方案1】:

如果您想在单个语句中执行此操作,可以使用:

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

或者,如果您有很多这样的替换,最好将它们存储在某种数据结构中,例如二维数组。例如:

//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);

【讨论】:

【解决方案2】:

当然它会打印两行:您有两个打印语句。使用此代码:

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;
    }
}

【讨论】:

  • 嗨,这个代码真的是我需要的,我太感谢你了 :)
  • @shumaila:那你可以点击我答案左边的勾来接受这个答案。当然,您也可以随时投票:D
  • 这在您必须将所有出现的cm 替换为call me 时不起作用,反之亦然。
【解决方案3】:

以上所有答案都可能是正确的。但是,将每个字符串替换一次并不有效。以下来自 Apache Commons StringUtils 的代码将帮助它一次有效地替换所有字符串。

    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"});

请注意:上述方法不适用于替换先前替换结果中的单词。例如:

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

将给出结果:“dcte”

【讨论】:

    【解决方案4】:

    现在您可以使用 commons-lang3 包中的 StringUtils。

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

    代码如下:

    strOutput = StringUtils.replaceEach(inString, new String[]{"call me", "as soon as possible"}, new String[]{"cm", "asap"});
    

    【讨论】:

      【解决方案5】:

      而不是使用 replace 使用 replaceAll 这对我有用

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

      【讨论】:

        【解决方案6】:

        使用System.out.print() 代替System.out.println()

        【讨论】:

          【解决方案7】:
              String strOutput1 = inString.replace("as soon as possible","asap");
          

          你应该把它改成

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

          【讨论】:

            【解决方案8】:

            使用 MessageFormat.format(queryString, retrieveArguments().toArray());

            【讨论】:

              【解决方案9】:

              根本问题是,一旦您进行了第一次替换,您就无法再次使用最初给定的相同字符串。 我认为正确的做法是使用不同的副本,并在替换内容时将内容从一个副本移动到另一个副本 可能比这更好,解决方案可能只是在每次完成替换后进行额外替换以擦除已经替换的模式。 ;)

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-01-17
                • 1970-01-01
                • 2017-01-22
                • 2014-02-01
                • 1970-01-01
                • 2016-08-29
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多