문장에서 단어의 총 발생 횟수를 찾으려고합니다. 내가 문자열이 두 개 (공간 분할)로 분할해야한다 "스택 솔루션"와 같은 문자열을 전달하고 문장에서 각 문자열의 발생의 더 찾을 필요가 없습니다입니다 경우문장에서 한 단어의 총 발생 수를 얻는 방법
String str = "This is stackoverflow and you will find great solutions here.stackoverflowstackoverflow is a large community of talented coders.It hepls you to find solutions for every complex problems.";
String findStr = "hello World";
String[] split=findStr.split(" ");
for(int i=0;i<split.length;i++){
System.out.println(split[i]);
String indexWord=split[i];
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
lastIndex = str.indexOf(indexWord,lastIndex);
System.out.println(lastIndex);
if(lastIndex != -1){
count ++;
lastIndex += findStr.length();
}
}
System.out.println("Count for word "+indexWord+" is : "+count);
}
: 나는 다음 코드를 시도 . 내가 한 단어 만 통과하면 카운트가 완벽합니다. 코드는 검색된 문자열을 포함하는 하위 문자열까지도 일치해야합니다. 예 : "스택"apperars에서 세 번 있지만 카운트는 2입니다.
고마워.
'lastIndex + = findStr.length();를'lastIndex + = indexWord.length();'로 대체 하시겠습니까? – qxz
great.its 잘 작동합니다. 시간을 절약 해 주셔서 감사합니다. –
답변을 추가하여이 질문을 해결 된 것으로 표시 할 수 있습니다. – qxz