Count Occurrences of a Character Inside a String in C#

  1. Count Occurrences of a Character in a String With the Linq Method in C
  2. Count Occurrences of a Character in a String With the String.Split() Method in C
  3. Count Occurrences of a Character in a String With the foreach Loop in C

This tutorial will introduce the methods to get the number of occurrences of a character in a string variable in C#.

Count Occurrences of a Character in a String With the Linq Method in C

Linq integrates SQL functionalities on data structures in C#. The following code example shows us how we can get the number of occurrences of a character in a string with the Linq method in C#.

using System;
using System.Linq;

namespace count_occurrences_of_a_char_in_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "/once/upon/a/time/";
            int count = source.Count(f => f == 'o');
            Console.WriteLine(count);
        }
    }
}

Output:

2

In the above code, we calculated the number of occurrences of the character o in the string variable source with the Linq method in C#.

Count Occurrences of a Character in a String With the String.Split() Method in C

The String.Split() method splits a string into multiple substrings based on a separator in C#. The String.Split(x) method would return 1 string more than the number of occurrences of x in the string. We can count the number of strings returned by the String.Split() method and subtract 1 from it to get the number of occurrences of the character inside the main-string. The following code example shows us how we can count the number of occurrences of a character in a string variable with the String.Split() method in C#.

using System;
using System.Linq;

namespace get_first_char_of_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "/once/upon/a/time/";
            int count = source.Split('o').Length - 1; 
            Console.WriteLine(count);
        }
    }
}

Output:

2

In the above code, we calculated the number of occurrences of the character o in the string variable source with the String.Split() function in C#.

Count Occurrences of a Character in a String With the foreach Loop in C

The foreach loop is used to iterate through a data structure in C#. We can use the foreach loop to iterate through each character of our string variable and check whether the character matches our desired character with an if statement in C#. The following code example shows us how we can count the number of occurrences of a character inside a string with the foreach loop in C#.

using System;
using System.Linq;

namespace get_first_char_of_string
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "/once/upon/a/time/";
            int count = 0;
            foreach (char c in source){
                if (c == 'o'){
                    count++;
                }
            }
            Console.WriteLine(count);
        }
    }
}

Output:

2

In the above code, we calculated the number of occurrences of the character o in the string variable source with the foreach loop in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp String

  • Convert List<string> to String in C#
  • Related Article - Csharp Char

  • Add Newline to String in C#
  • Convert String to Hex in C#