
A substring may occur several times in a string. We can count the number of occurrences of one string in another, larger string. This is useful for validity checks in text and markup. It helps with checking that only one tag string is found in your HTML.
Input strings: Sam's first name is Sam. Dot Net Perls is about Dot Net No duplicates here aaaa Words: Sam Net here aa Counts: 2 2 1 2

Here is an easy method that uses IndexOf and a loop to count up the number of occurrences of a specified string in another string. It receives two parameters, the first being the source string, and the second the string you want to count.
This C# example program shows how to count the occurrences of a string.
Program that counts strings [C#]
using System;
class Program
{
static void Main()
{
string s1 = "Sam's first name is Sam.";
string s2 = "Dot Net Perls is about Dot Net";
string s3 = "No duplicates here";
string s4 = "aaaa";
Console.WriteLine(TextTool.CountStringOccurrences(s1, "Sam")); // 2
Console.WriteLine(TextTool.CountStringOccurrences(s1, "cool")); // 0
Console.WriteLine(TextTool.CountStringOccurrences(s2, "Dot")); // 2
Console.WriteLine(TextTool.CountStringOccurrences(s2, "Net")); // 2
Console.WriteLine(TextTool.CountStringOccurrences(s3, "here")); // 1
Console.WriteLine(TextTool.CountStringOccurrences(s3, " ")); // 2
Console.WriteLine(TextTool.CountStringOccurrences(s4, "aa")); // 2
}
}
/// <summary>
/// Contains static text methods.
/// Put this in a separate class in your project.
/// </summary>
public static class TextTool
{
/// <summary>
/// Count occurrences of strings.
/// </summary>
public static int CountStringOccurrences(string text, string pattern)
{
// Loop through all instances of the string 'text'.
int count = 0;
int i = 0;
while ((i = text.IndexOf(pattern, i)) != -1)
{
i += pattern.Length;
count++;
}
return count;
}
}
Program overview. The CountStringOccurrences method is a static method, meaning it doesn't need to store state, and it occurs in a static class, which is simply a convenient place for helper methods.
Details. In the Main method, which we use to test the CountStringOccurrences method, we see that "Sam" occurs twice in the example string, matching the requirement. It is usually best to simplify the branches in your code, avoiding as many conditionals/ifs as possible. This loop only tests one condition, the result of IndexOf.
Alternatives. It isn't an extension method primarily because the syntax isn't important to me, but you are free to adapt it with the this keyword. You could count overlapping occurrences by changing the increment on i to "i++" instead of "i += pattern.Length".
MSDN. Microsoft has an MSDN article on how to count instances of a word with LINQ, but their approach is likely slow. It is confusing—I would never use it in an important program.
MSDN reference
We saw how to count occurrences of one, smaller string in another, larger string using the C# programming language. Importantly, we only loop once over the source, which reduces the cost of the method. In my application, I use this method to do simple validation of HTML, such as testing that only one "<title>" element occurs. This results in higher document quality.
Algorithms