我有一个看起来像这样的字符串:
"Hello my is Joeseph. It is very nice to meet you. What a wonderful day it is!".
Run Code Online (Sandbox Code Playgroud)
我想计算is字符串中的次数.
我怎么能用Java做到这一点?
Mik*_*x6r 30
一种简单的方法是使用Apache StringUtils countMatches
StringUtils.countMatches("Hello my is Joeseph. It is very nice to meet you. What a wonderful day it is!", "is");
Run Code Online (Sandbox Code Playgroud)
asg*_*sgs 11
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);
Run Code Online (Sandbox Code Playgroud)