C# program to count occurrences of a word in string

Counting occurrences of a specific word in a string is a common string manipulation task in C#. This can be achieved using various approaches like the IndexOf() method, regular expressions, or LINQ methods.

Using IndexOf() Method

The IndexOf() method searches for the first occurrence of a substring and returns its position. By combining it with a loop, we can count all occurrences −

using System;

class Program {
   static void Main() {
      string str = "Hello World! Hello Universe! Hello!";
      string word = "Hello";
      int count = CountOccurrences(str, word);
      Console.WriteLine("String: " + str);
      Console.WriteLine("Word to find: " + word);
      Console.WriteLine("Occurrences: " + count);
   }

   public static int CountOccurrences(string text, string word) {
      int count = 0;
      int index = 0;
      
      while ((index = text.IndexOf(word, index)) != -1) {
         index += word.Length;
         count++;
      }
      return count;
   }
}

The output of the above code is −

String: Hello World! Hello Universe! Hello!
Word to find: Hello
Occurrences: 3

Using Split() Method

Another approach is to split the string using the target word as delimiter and count the resulting parts −

using System;

class Program {
   static void Main() {
      string str = "apple pie apple juice apple tart";
      string word = "apple";
      
      string[] parts = str.Split(new string[] { word }, StringSplitOptions.None);
      int count = parts.Length - 1;
      
      Console.WriteLine("String: " + str);
      Console.WriteLine("Word: " + word);
      Console.WriteLine("Occurrences: " + count);
   }
}

The output of the above code is −

String: apple pie apple juice apple tart
Word: apple
Occurrences: 3

Case-Insensitive Word Counting

To count occurrences regardless of case, use StringComparison.OrdinalIgnoreCase

using System;

class Program {
   static void Main() {
      string str = "Java programming java tutorial JAVA language";
      string word = "java";
      int count = CountOccurrencesIgnoreCase(str, word);
      
      Console.WriteLine("String: " + str);
      Console.WriteLine("Word (case-insensitive): " + word);
      Console.WriteLine("Occurrences: " + count);
   }

   public static int CountOccurrencesIgnoreCase(string text, string word) {
      int count = 0;
      int index = 0;
      
      while ((index = text.IndexOf(word, index, StringComparison.OrdinalIgnoreCase)) != -1) {
         index += word.Length;
         count++;
      }
      return count;
   }
}

The output of the above code is −

String: Java programming java tutorial JAVA language
Word (case-insensitive): java
Occurrences: 3

How It Works

The IndexOf() method starts searching from a specified position and returns the index of the first match. When a match is found, we increment the search position by the length of the word to avoid counting overlapping matches, then continue searching from that new position.

IndexOf() Search Process "Hello World! Hello Universe! Hello!" 1st match index = 0 2nd match index = 13 3rd match index = 27

Conclusion

Counting word occurrences in C# can be accomplished using IndexOf() in a loop, splitting the string, or regular expressions. The IndexOf() approach is efficient and provides flexibility for case-sensitive or case-insensitive searches, making it the most commonly used method.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements