문자열에서 특정 단어를 제거하는 방법을 알아내는 데 어려움이 있습니다. 기본적으로 String이 있습니다. 문자열의 모든 단어를 미리 배열 된 단어 수와 비교합니다. 문자열의 단어가 사전 설정된 단어 중 하나와 일치하면 해당 단어를 문자열에서 제거합니다. 내가 문자열을 가지고 예를 들어 문자열에서 특정 단어 제거
이 방법을 실행 한 후 나는 단어 배열해야한다 "테스트 문장"{ "테스트", "문장"} 여기에 내가 지금까지 ... 이 무엇편집 은 기본적으로 문제는 아무것도 내가 끝낼, 변화 없다는 것입니다 { "이다", "A", "테스트", "문장"}
private void fillerWords(){
String[] commonWords = {"the","of","to","and","a","in","is","it","you","that","he","was","for","on","are","with","as","i"};
List <String>wordList = new ArrayList<String>(Arrays.asList(commonWords));
//Split words in sentence up by word, put them into array
String s = "is a test sentance";
String[] tArray = s.split(" ");
List <String>list = new ArrayList<String>(Arrays.asList(tArray));
//take out words
for(int i=0; i<list.size(); i++){
//Check to see if a sentence word is a common word, if so remove word
for(int c=0; c<wordList.size(); c++){
if(wordList.get(c) == list.get(i)){
list.remove(i);
}//end if
}//end for
}//end for
for(int x=0; x<list.size(); x++){
System.out.printf("%s %s \n", x, list.get(x));
}
}
}
을 당신은 단어 { "테스트"와 끝까지해야한다고 :
이 같은 당신의 비교 블록을 변경: 또한
, 여기에 많은 변화없이 문제를 해결하는 간단한 방법 , "sentence"}하지만 실제로 끝내고있는 것은 무엇입니까? – Mercurybullet이 문제가 무엇입니까? 대략 말하면, 그것이 효과가있는 것처럼 보입니다. 그러나 내부 루프를 저장하기 때문에 더 나은 런타임 성능을 제공하는 Stopwords에 HashSet을 사용해야합니다. – Jochen
내가 끝나는 것은 { "is", "test", "sentence"}입니다. 기본적으로 아무 것도 없습니다 – SNV7