2011-07-19 58 views
6

我正在写一个程序,将取代单个字符串中的多个单词。我正在使用这段代码,但它取代了单词,但给出了两条不同的结果。我想要替换多个单词并在一行中输出。如何在Java中用单个字符串替换多个单词?

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

    } 
} 
+0

类似的问题:http://stackoverflow.com/q/1326682/18511 – Kip

回答

0

使用System.out.print()而不是System.out.println()

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

你应该改变,要

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

你好,这是非常有用的:) – shumaila

4

当然它打印两行:你有两个打印语句。如果你想这样做在一个语句就可以使用

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

嗨,这是代码真的是我需要的一个,我是soooooooooooooooo感谢你:) – shumaila

+1

@shumaila:然后你可以点击我答案左边的刻度来接受这个答案。当然,你也可以随时加入:D –

12

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

或者,如果你有很多这样的替代品,它可能是明智的将它们存储在一些使用此代码。一种数据结构,如2d数组。例如:

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

+1:这确实是一种整洁的方式。 –

+4

这样做是不正确的,因为如果你输入''abc''和'replacements = {{“a”} {“b”},{“b”} {“c”}}'你应该期待''“ bcc“'输出,但你会得到''ccc”'。解决方案在这里描述 - [stackoverflow.com/questions/1326682/java-replacing-multiple-different-substring-in-a-string-at-once-or-in-the-most](http://stackoverflow。 COM /问题/ 1326682/java的替换-多不同子串-IN-A-串一次刻录或 - 内式最)。 –

0

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

0

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

0

而不是使用replace使用replaceAll这工作对我来说

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

以上所有的答案可能是正确的。但是,一次更换每个字符串效率不高。以下代码来自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”

相关问题