1

我遇到了字符串问题,我需要一个解决方案我试图用在同一位置找到的字符替换在某个位置找到的字符,例如

 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;
}

因此,如果用户输入 a 将其替换为 t 并且如果用户输入 b 将其替换为 u 并且如果用户输入 c 它将被替换为 v 并且只有按照该顺序才有可能,并且如果它向我展示了它的正确方式应该做的

4

1 回答 1

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格式化字符串,它是一个字符串。

于 2012-11-19T08:03:19.243 回答