2011-07-19 59 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」

相關問題