我遇到字符串问题,我需要一个解决方案我试图用在同一位置找到的字符替换在某个位置找到的字符,例如
private String wordNormalize(String enteredWord,String dictionary){
String normalizedWord = null;
// remove empty spaces at beginning and at the end of the word and change to lower case
normalizedWord = enteredWord.trim().toLowerCase();
//normalize term, removing all punctuation marks
normalizedWord = normalizedWord.replaceAll("["+punctuationMarks2+"]", "[b,v]");
//normalize word removing to character if dictionary has english lang
normalizedWord = normalizedWord.replaceFirst("to ", " ");
//normalizeWord if dictionary has german
if(normalizedWord.length() > 0){
normalizedWord.replace("a,b,c","t,u,v");
/*for(int i = 0;i<normalizedWord.length();i++){
char currentChar = normalizedWord.charAt(i); // currently typed character
String s1= Character.toString(currentChar);
for(int j = 0;j<specialCharacters.length;j++){
s1.replaceAll("[ "+specialCharacters[i]+" ]",""+replaceCharactersDe[i]+"");
}
= str.replace("a,b,c","t,u,v");
}*/
}
//normalize term removing special characters and replacing them
/*for(int i = 0; i > specialCharacters.length;i++){
if(normalizedWord.equals(specialCharacters[i])){
normalizedWord = replaceCharactersDe[i];
}
}*/
return normalizedWord;
}
因此,如果用户输入了替换为t,并且如果用户输入b,则将其替换为u,如果用户输入c,则将替换为v,并且只有按此顺序这是可能的,如果它显示给我正确的方式它应该完成
答案 0 :(得分:1)
我不清楚你想要用什么方法
normalizedWord = normalizedWord.replaceAll("["+punctuationMarks2+"]", "[b,v]");
这似乎不对,但我不知道如何修复它,因为我不知道它正在尝试做什么。我想你要找的是
normalizedWord = normalizedWord.replaceAll("\\p{Punct}", "");
另一方面,你什么都不做,因为字符串是不可变的。你想做类似
的事情 normalizedWord = normalizedWord.replace("a,b,c","t,u,v");
但是这将用字符串"a,b,c"
替换所有出现的子串"t,u,v"
-
你想要的是:
normalizedWord = normalizedWord.replace('a', 't');
normalizedWord = normalizedWord.replace('b', 'u');
normalizedWord = normalizedWord.replace('c', 'v');
我们可以使用更通用的解决方案,但您必须向我们展示如何格式化dictionary
(字符串)。