2 回答
TA贡献1828条经验 获得超3个赞
试试这个代码:
public class JavaApplication22 {
public static void main(String[] args) {
String str = "Football is a great game It is most popular game all over the world It is not only a game but also a festival of get together for the nations which is most exciting too";
String pattern = "is";
int count = 0;
int a = 0;
while((a = str.indexOf(pattern, a)) != -1){
a += pattern.length();
count++;
}
System.out.println("Count is : " + count);
}
}
TA贡献1829条经验 获得超6个赞
创建一个具有所需长度的子字符串,然后使用 linq 进行计数。
int length = 100;
string word = "is";
string sentence = "Football is a great game It is most popular game all over the world It is not only a game but also a festival of get together for the nations which is most exciting too";
//Substring with desired length
sentence = sentence.Substring(0, length);
int count = 0;
//creating an array of words
string[] words = sentence.Split(Convert.ToChar(" "));
//linq query
count = words.Where(x => x == word).Count();
Debug.WriteLine(count);
对于第二部分,创建一个从 100 开始到字符串末尾的子字符串。
- 2 回答
- 0 关注
- 112 浏览
添加回答
举报