Can someone recommend me a good code/algorithm for string searching?
I'm using Java.
Example: How many times the word apple appears in a text.
Can someone 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
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.