2012-11-19 2 views
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; 
} 

그래서 사용자가 t로 대체 된 문자를 입력하고 사용자가 b를 입력하면 그 문자가 u로 대체되고 사용자가 c로 입력하면 c가이 문자로 교체되며이 순서 대로만 가능합니다. 옳은 길 그 일을해야한다고

+1

[String.replace (char, char)] (http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replace%28char,%20char%29) – nhahtdh

+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이 어떻게 포맷되는지를 보여 주어야합니다.

+0

죄송합니다.이 부분을 잊지 않으 셨습니다. normalizedWord = normalizedWord.replaceAll ("["+ "punctuationMarks2 +"] ","[b, v] "); 나는 이미이 부분을 통제하고 있기 때문에 마지막 부분은 실제 문제이지만 그것이 내게 준 답 이외에 가능한 답이 아닌 것으로 보인다. 각 문자에 대한 대체 방법을 반복하지 않고서는 다른 방법을 사용할 수있다. 나는 – user1534409

+0

을 확실히 바꾸고 싶다. for 루프에 넣고 첫 번째 문자를 대체 할 문자로 바꾸고 두 번째 문자를 대체 문자로 바꾼다. – jlordo