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("is","was");
System.out.println(strOutput);
}
}
我正在尝试用输入的行替换所有出现的“is”和“was”,我能够做到这一点,但我不想弄乱“is”是单词的一部分。< / p>
例如,任何出现的单词名称都应该被忽略。只有“是”的个别出现应该用“是”替换
我知道我可以使用正则表达式,但我不知道格式是否正则表达式
答案 0 :(得分:5)
尝试
String strOutput = inString.replaceAll("\\bis\\b","was");
测试输出:
Input String:
is island is feris kiss
输出
was island was feris kiss
答案 1 :(得分:0)
不是我对java了解多少,但你可以尝试添加空格。理论上应该有效。
System.out.println("Input String:\n");
Scanner keyboardScanner = new Scanner(System.in);
String inString = keyboardScanner.nextLine();
String strOutput = inString.replace(" is "," was ");
System.out.println(strOutput);