Results 1 to 6 of 6
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default find the no. of occurance's

    Hi, is it possible to count the number of times a word appears in a given string?
    e.g String word= "sony";
    String sentence="sony ericsson is a leading company in mobile. sony was found in oct 2001";

    In this case the o/p should be 2.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Sure, there are many ways to get the number of times the word appears!
    Java Code:
    		int count = 0; 
    		for (Matcher m = Pattern.compile(word).matcher(sentence); m.find(); ++count);
    		System.out.println(count);
    Java Code:
    		int count=0;
    		for (int i = 0; i != -1; i = sentence.indexOf(word, i+word.length()), ++count);
    		System.out.println(count);
    .....

    be creative :D

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    You can even do it without loops; have a look:

    Java Code:
    public int countOf(String s, String pattern) {
       String result= s.replaceAll(pattern, "");
       return (s.length()-result.length())/pattern.length();
    }
    To repeat the previous poster: be creative.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Oh boy! No doubt that I call you a Java guru :)

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    You can even do it without loops; have a look:

    Java Code:
    public int countOf(String s, String pattern) {
       String result= s.replaceAll(pattern, "");
       return (s.length()-result.length())/pattern.length();
    }
    To repeat the previous poster: be creative.

    kind regards,

    Jos

    Unless OP has no problem with it, a word like "sonya" will get counted with this solution, if the searched word is "sony".

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaHater View Post
    Unless OP has no problem with it, a word like "sonya" will get counted with this solution, if the searched word is "sony".
    Yep, same as with the other solutions given so far. If the OP doesn't want that. a more elaborate test (e.g. word boundaries in a RE or leading/trailing non-letter characters around the word) has to be applied; but let's start with Occam's razor.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. where to find
    By aizen92 in forum New To Java
    Replies: 6
    Last Post: 02-08-2011, 01:52 PM
  2. Cannot find symbol
    By TripleZigZag in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 02:36 AM
  3. can't find tamplate.tld anywhere
    By amu in forum Advanced Java
    Replies: 1
    Last Post: 10-15-2008, 02:15 PM
  4. Can't find class
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 05:45 AM
  5. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •