-3

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.

Smi
  • 13,850
  • 9
  • 56
  • 64
karq
  • 849
  • 2
  • 14
  • 27
  • Just a note, algorithms do not necessary depend upon the language. What you want is a good implementation as others have hinted at in the answers section. – demongolem Jun 28 '11 at 03:54

5 Answers5

3

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

Community
  • 1
  • 1
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
3

This guy has gone to the trouble of implementing some advanced String search algorithms in Java.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

I'd recommend regexp

Giann
  • 3,142
  • 3
  • 23
  • 33
0

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.

borrible
  • 17,120
  • 7
  • 53
  • 75
0

Check the wiki page if you are looking for more advanced algorithms.

abhiasawa
  • 1,330
  • 1
  • 10
  • 20