我试图找到一个字符串中的单词。但是,由于一段时间它不能识别一个词。我试图删除标点符号,但它似乎没有效果。我在这里错过了什么吗?这是我正在使用的代码行: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