Java在字符串中查找单词 [英] Java Find word in a String
本文介绍了Java在字符串中查找单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在HTML源代码中找到一个单词.我还需要计算发生的次数.我正在尝试使用正则表达式.但它说找到0个匹配项.
I need to find a word in a HTML source code. Also I need to count occurrence. I am trying to use regular expression. But it says 0 match found.
我正在使用正则表达式,因为我认为这是最好的方法.如果有更好的方法,请告诉我.
I am using regular expression as I thought its the best way. In case of any better way, please let me know.
我需要在HTML源代码中查找单词"hsw.ads"的出现.
I need to find the occurrence of the word "hsw.ads" in HTML source code.
我已采取以下步骤.
int count = 0;
{
Pattern p = Pattern.compile(".*(hsw.ads).*");
Matcher m = p.matcher(SourceCode);
while(m.find())count++;
}
但计数为0;
请让我知道您的解决方案.
Please let me know your solutions.
谢谢.帮助搜索者
推荐答案
您应该尝试一下.
private int getWordCount(String word,String source){
int count = 0;
{
Pattern p = Pattern.compile(word);
Matcher m = p.matcher(source);
while(m.find()) count++;
}
return count;
}
在字符串中传递要搜索的单词(非模式).
Pass the word (Not pattern) you want to search in a string.
这篇关于Java在字符串中查找单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文