英文:
String.replace() is replacing one of two strings only
问题
我有这行文字:
> 美国在越南南部驻扎的部队数量,与驻扎在西德的部队数量相比。
我想做的是检查这行“文字”中是否有任何可能在组合时具有意义的单词。
例如:将字符串 viet nam
替换为正确的词语 vietnam
。
我编写了这个函数:
private String correctLine(ArrayList<String> newWords, String line) {
line = line.toLowerCase();
String[] words = line.split(" ");
for (int i = 0; i < newWords.size(); i++) {
for (int j = 0; j < words.length; j++) {
if(newWords.get(i).toLowerCase().equals(words[j].concat((j + 1 == words.length)?"":words[j+1]))){
line = line.replace(words[j].toLowerCase(), newWords.get(i).toLowerCase());
line = line.replace(words[j+1].toLowerCase(), "");
}
}
}
return line;
}
这个函数的作用是接收一个将在文本中替换的单词列表 newWords
,以及目标文本行 line
。
问题:
当满足 if
条件时:
vietnam
等于 words[j]
(viet)和 words[j+1]
(nam)
我替换了两次 line
:
- 用
vietnam
替换viet
。 - 用空字符串替换
nam
。
但问题是其中只有一个被替换,另一个被忽略。
-
如果我注释掉第一个
replace
,结果是:number of troops the united states has stationed in south viet as compared with the number of troops it has stationed in west germany .
-
如果我注释掉第二个
replace
,结果是:number of troops the united states has stationed in south vietnam nam as compared with the number of troops it has stationed in west germany .
你对这种情况有什么想法吗?
英文:
I have this line of text:
> number of troops the united states has stationed in south viet nam as compared with the number of troops it has stationed in west germany .
And what i'm trying to do is check the line of text for any words that may have a meaning when combined.
For example: replace the viet nam
String with the correct word vietnam
.
I wrote this function:
private String correctLine(ArrayList<String> newWords, String line) {
line = line.toLowerCase();
String[] words = line.split(" ");
for (int i = 0; i < newWords.size(); i++) {
for (int j = 0; j < words.length; j++) {
if(newWords.get(i).toLowerCase().equals(words[j].concat((j + 1 == words.length)?"":words[j+1]))){
line = line.replace(words[j].toLowerCase(), newWords.get(i).toLowerCase());
line = line.replace(words[j+1].toLowerCase(), "");
}
}
}
return line;
}
What this function do is it takes a List of words newWords
that will be replaced in the text, and the target line of text line
.
The problem:
When the if
condition is met:
vietnam
is equal to words[j]
(viet) & words[j+1]
(nam)
I replace the line
twice:
- replace
viet
withvietnam
. - replace
nam
with
But what happens is only one of them is being replaced, while the other is ignored.
-
If i comment the first
replace
the result is:number of troops the united states has stationed in south viet as compared with the number of troops it has stationed in west germany .
-
If i comment the second
replace
the result is:number of troops the united states has stationed in south vietnam nam as compared with the number of troops it has stationed in west germany .
Any idea on why this is happening?
答案1
得分: 2
luk2302是正确的,尽管使用该方法会导致出现两个连续的空格,这也必须解决。我可以提供另一种更简单且更灵活的方法吗?
Map<String, String> replace = new HashMap<>();
replace.put("viet nam", "vietnam");
String line = "number of troops the united states has stationed in south viet nam as compared with the number of troops it has stationed in west germany .";
for (String key : replace.keySet()) {
line = line.replaceAll(key, replace.get(key));
}
英文:
luk2302 is correct, although you will end up with two consecutive spaces with that approach which has to be addressed as well. Might I suggest another approach which is simpler and more flexible?
Map<String, String> replace = new HashMap<>();
replace.put("viet nam", "vietnam");
String line = "number of troops the united states has stationed in south viet nam as compared with the number of troops it has stationed in west germany .";
for (String key : replace.keySet()) {
line = line.replaceAll(key, replace.get(key));
}
答案2
得分: 1
你应该仔细考虑关于替换内容的两个陈述,如果你用 vietnam
替换 viet
,然后再将 nam
替换为空,那么显然你会再次替换刚刚替换的 vietnam
中的 nam
。结果就是 viet nam
-> vietnam nam
-> viet nam
。
也许通过简单地交换这两个陈述,你可以避免这个问题:
line = line.replace(words[j+1].toLowerCase(), "");
line = line.replace(words[j].toLowerCase(), newWords.get(i).toLowerCase());
英文:
You should think very hard about the two statements about what you replace, if you replace viet
with vietnam
and then replace nam
with nothing then you obviously will replace the nam
of the just replaced vietnam
again. Resulting in viet nam
-> vietnam nam
-> viet nam
.
You might get away by simply switching the two statements around:
line = line.replace(words[j+1].toLowerCase(), "");
line = line.replace(words[j].toLowerCase(), newWords.get(i).toLowerCase());
答案3
得分: 1
不需要这两个`replace`语句。
下面的代码经过测试,运行良好。
public class Main {
public static void main(String args[]) {
String line = "in south viet nam as compared with the number of troops it has stationed in west germany";
ArrayList<String> newWords = new ArrayList<>();
newWords.add("vietnam");
newWords.add("India");
line = line.toLowerCase();
String[] words = line.split(" ");
for (int i = 0; i < newWords.size(); i++) {
for (int j = 0; j < words.length; j++) {
if(newWords.get(i).toLowerCase().equals(words[j].concat((j + 1 == words.length)?"":words[j+1]))){
line = line.replace((words[j] + " " + words[j+1]), newWords.get(i).toLowerCase());
}
}
}
System.out.println(line);
}
}
输出:
in south vietnam as compared with the number of troops it has stationed in west germany
英文:
You don't actually need the two replace
.
Below code is tested and working fine.
public class Main {
public static void main(String args[]) {
String line = "in south viet nam as compared with the number of troops it has stationed in west germany";
ArrayList<String> newWords = new ArrayList<>();
newWords.add("vietnam");
newWords.add("India");
line = line.toLowerCase();
String[] words = line.split(" ");
for (int i = 0; i < newWords.size(); i++) {
for (int j = 0; j < words.length; j++) {
if(newWords.get(i).toLowerCase().equals(words[j].concat((j + 1 == words.length)?"":words[j+1]))){
line = line.replace((words[j] + " " + words[j+1]), newWords.get(i).toLowerCase());
}
}
}
System.out.println(line);
}
}
Output :
in south vietnam as compared with the number of troops it has stationed in west germany
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论