如何计算Java字符串中出现序列的次数? [英] How do I count the number of times a sequence occurs in a Java string?

查看:52
本文介绍了如何计算Java字符串中出现序列的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的字符串:

I have a String that looks like:

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

我想计算字符串中 is 的次数.

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

如何用Java做到这一点?

How can I do this in Java?

推荐答案

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);

这篇关于如何计算Java字符串中出现序列的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆