Can someon开发者_高级运维e recommend me a good code/algorithm for string searching?
I'm using Java.
Example: How many times the word apple appears in a text.
You can try calling method countMatches(source,word)
of org.apache.commons.lang.StringUtils
.
A small example:
String string = "How arer you? Who are you?";
System.out.println(StringUtils.countMatches(string,"you"));
Also look at this thread: Java Counting # of occurrences of a word in a string
This guy has gone to the trouble of implementing some advanced String search algorithms in Java.
I'd recommend regexp
Several algorithms are detailed on the wikipedia string searching algorithm page. I'd question whether you should be looking at string searching algorithms or just using the functionality already provided by the language.
Check the wiki page if you are looking for more advanced algorithms.
精彩评论