11

I have a String that looks like:

"Hello my is Joeseph. It is very nice to meet you. What a wonderful day it is!". 

I want to count the number of times is is in the string.

How can I do this in Java?

4
  • 2
    Could you post what you've tried yourself? And what about: "Is miss bliss?"
    – Bart Kiers
    Mar 7, 2011 at 18:54
  • @Bart Kiers then Im sure we could just look for " is " instead of "is" to avoid that ;)
    – Joeseph
    Mar 7, 2011 at 19:23
  • 1
    "you don't understand what it is."
    – asgs
    Mar 7, 2011 at 19:24
  • By looking for " is ", you'll miss: " is," and "is!" etc.
    – Bart Kiers
    Mar 7, 2011 at 19:24

7 Answers 7

33

An easy way is using Apache StringUtils countMatches

StringUtils.countMatches("Hello my is Joeseph. It is very nice to meet you. What a wonderful day it is!", "is");
3
  • 1
    or with Spring Framework StringUtils.countOccurrencesOf(string, "is");
    – stivlo
    Sep 12, 2011 at 21:11
  • 1
    Aren’t Spring StringUtils meant primarily for use within the framework? Meaning, it works, but Apache’s clearly better in this respect. Sep 2, 2013 at 19:49
  • Works nicely. Thanks. Sep 25, 2014 at 14:59
15
int index = input.indexOf("is");
int count = 0;
while (index != -1) {
    count++;
    input = input.substring(index + 1);
    index = input.indexOf("is");
}
System.out.println("No of *is* in the input is : " + count);
7
  • @jzd agreed, thanks for pointing out the mistake.
    – asgs
    Mar 7, 2011 at 19:01
  • @asgs, I have removed my down vote. Thanks for the fix.
    – jzd
    Mar 7, 2011 at 19:02
  • 1
    @asgs, you can't undelete comments and because you edited your answer within the first five minutes there is not revision history. So there is nothing to learn.
    – jzd
    Mar 7, 2011 at 19:09
  • 2
    You could also use index = input.indexOf("is", index+1) instead of substring and then indexOf. W/o profiling, I don't know for sure, but suspect this will be quicker. It's also 1 line less of code ;)
    – Mikezx6r
    Mar 7, 2011 at 19:26
  • 2
    Many false positives like "miss", "bliss". It also misses the capitalized "Is", "IS" or "iS". So although the OP has accepted the answer, a -1 from me.
    – Bart Kiers
    Mar 7, 2011 at 19:28
4

If you prefer regex, here is a regex solution:

String example = "Hello my is Joeseph. It is very nice to meet you. isWhat a wonderful day it is!";
Matcher m = Pattern.compile("\\bis\\b").matcher(example);

int matches = 0;
while(m.find())
    matches++;

System.out.println(matches);

In this case the "is" in "isWhat" is ignored, because of the \b boundary matcher in the pattern

2
String haystack = "Hello my is Joeseph. It is very nice to meet you. What a wonderful day it is!";
haystack.toLowerCase();
String needle = "is";

int numNeedles = 0;

int pos = haystack.indexOf(needle);

    while(pos >= 0 ){

      pos = pos + 1;
      numNeedles = numNeedles + 1;

      pos = haystack.indexOf(needle,pos);

    }

 System.out.println("the num of " +needle+ "= " +numNeedles);
2
  • 1
    Since when is a variable of the char type able to hold multiple characters? Sep 5, 2013 at 6:22
  • Noted my mistake and edited
    – Betsy
    Sep 6, 2013 at 8:25
1

split on every " " (blank) and check the outcoming string[] with a loop

2
  • What if there was a space in the string he wanted to check for? Mar 7, 2011 at 19:15
  • In that case he wouldn't count the number of "is" in the string >_< , but if so, he could use a regex.
    – nyyrikki
    Mar 7, 2011 at 19:27
1

You can find the code here. It strangely looks like the Robby's one.

1

This takes into account the length of "replace"

String text = "a.b.c.d";
String replace = ".";
int count = (text.length()- (text.replaceAll(replace, "").length())) / replace.length();
System.out.println(count)

Not the answer you're looking for? Browse other questions tagged or ask your own question.