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 關注
- 150 瀏覽
添加回答
舉報