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?
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");
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With