如何在不替换单词之间的字符的情况下替换文本
String test = "This is the test String for replace is best";
txtReplaceText.setText(test.replace("is"," "));
这是我不想删除的输出“ 此”
答案 0 :(得分:0)
您可以使用regex
:
String test = "This is the test String for replace is best";
String regex = "(?<=^| )is(?= |$)";
String output = test.replaceAll(regex, " yourNewString ");
System.out.println(output);
(?<=^| )
匹配行首或空格(?= |$)
匹配行尾或空格您可以在正则表达式中添加更多字符,以满足"(?<=^| )is(?= |\\!|\\?|,|.|$)"
之类的需求,以适应句子的结尾。
它应该返回:
"This yourNewString the test String for replace yourNewString best"
最佳