[英]How to replace multiple words in a single string in Java?
我正在編寫一個程序,它將替換單個字符串中的多個單詞。 我正在使用此代碼,但它正在替換 word 但在兩行不同的行中給出結果。 我想在一行中替換多個單詞和 output。
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);
}
}
如果您想在單個語句中執行此操作,您可以使用:
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);
當然它會打印兩行:您有兩個打印語句。 使用此代碼:
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;
}
}
以上所有答案都可能是正確的。 但是,將每個字符串替換一次並不有效。 以下來自 Apache Commons StringUtils 的代碼將幫助它有效地替換 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"});
請注意:上述方法不適用於替換先前替換結果中的單詞。 例如:
StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"})
將給出結果:“dcte”
現在您可以在 commons-lang3 package 中使用 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"});
使用 MessageFormat.format(queryString, retrieveArguments().toArray());
根本問題是,一旦您進行了第一次替換,您就不能再次使用最初給定的字符串。 我認為正確的做法是使用不同的副本並在替換內容時從一個副本移動到另一個內容可能比這更好,解決方案可能只是在每次完成替換后進行額外替換以擦除已經替換的模式。 ;)
而不是使用replace
使用replaceAll
這對我有用
String strOutput = inString.replaceAll("call me","cm").replaceAll("as soon as possible","asap");
使用System.out.print()
而不是System.out.println()
String strOutput1 = inString.replace("as soon as possible","asap");
你應該把它改成
String strOutput1 = strOutput .replace("as soon as possible","asap");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.