पहले स्ट्रिंग सेट करें -
string str = "Hello World! Hello!";
अब "हैलो" शब्द के बार-बार आने के लिए स्ट्रिंग की जाँच करें और -
. के माध्यम से लूप करेंwhile ((a = str1.IndexOf(pattern, a)) != -1) {
a += pattern.Length;
count++;
} उदाहरण
आप स्ट्रिंग में किसी शब्द की बारंबारता गिनने के लिए निम्न कोड चलाने का प्रयास कर सकते हैं।
using System;
class Program {
static void Main() {
string str = "Hello World! Hello!";
Console.WriteLine("Occurrence:"+Check.CheckOccurrences(str, "Hello"));
}
}
public static class Check {
public static int CheckOccurrences(string str1, string pattern) {
int count = 0;
int a = 0;
while ((a = str1.IndexOf(pattern, a)) != -1) {
a += pattern.Length;
count++;
}
return count;
}
} आउटपुट
Occurrence:2