我試圖找到一個字符串中的單詞。但是,由於一段時間它不能識別一個詞。我試圖刪除標點符號,但它似乎沒有效果。我在這裏錯過了什麼嗎?這是我正在使用的代碼行:s.replaceAll(「([a-z] +)[?:!。,;] *」,「$ 1」);刪除標點符號問題
String test = "This is a line about testing tests. Tests are used to examine stuff";
String key = "tests";
int counter = 0;
String[] testArray = test.toLowerCase().split(" ");
for(String s : testArray)
{
s.replaceAll("([a-z] +) [?:!.,;]*","$1");
System.out.println(s);
if(s.equals(key))
{
System.out.println(key + " FOUND");
counter++;
}
}
System.out.println(key + " has been found " + counter + " times.");
}
我設法找到一個解決方案(儘管可能不是理想的)通過使用S = s.replaceAll( 「\ W」, 「」);感謝大家指導如何解決這個問題。
看看這個問題,選擇答案: [http://stackoverflow.com/questions/767759/occurences-of-substring-in-a-string][1] [1]:http://stackoverflow.com/questions/767759/occurences-of-substring-in-a-string – 2013-03-25 16:00:22