C# String Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To count the occurrences of a single character within a string in C#, you can use the Count
method along with a lambda expression that compares each character in the string to the target character. Here is an example:
string str = "hello world"; char targetChar = 'o'; int count = str.Count(c => c == targetChar); Console.WriteLine(count); // output: 2
To count the occurrences of multiple characters within a string, you can use a loop and the same approach as above. Here is an example:
string str = "hello world"; string targetChars = "lo"; int count = 0; foreach (char c in str) { if (targetChars.Contains(c)) { count++; } } Console.WriteLine(count); // output: 5
In this example, the foreach
loop iterates through each character in the string. If the character is one of the target characters specified in targetChars
, the count is incremented.
C# count occurrences of substring in string:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = mainString.Split(new[] { substringToCount }, StringSplitOptions.None).Length - 1; // Result: 4
Count specific word occurrences in C# string:
string sentence = "This is a sentence with words. This sentence has words."; string wordToCount = "sentence"; int occurrences = Regex.Matches(sentence, @"\b" + wordToCount + @"\b").Count; // Result: 2
C# string count occurrences of character:
string inputString = "programming"; char charToCount = 'r'; int occurrences = inputString.Count(c => c == charToCount); // Result: 2
How to find the number of occurrences of a substring in C#:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = (mainString.Length - mainString.Replace(substringToCount, "").Length) / substringToCount.Length; // Result: 4
C# count occurrences of a word in a sentence:
string sentence = "This is a sentence with words. This sentence has words."; string wordToCount = "sentence"; int occurrences = sentence.Split(' ').Count(w => w.Equals(wordToCount, StringComparison.OrdinalIgnoreCase)); // Result: 2
LINQ count string occurrences in C#:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = mainString.Split(new[] { substringToCount }, StringSplitOptions.None).Length - 1; // Result: 4
Count occurrences of a specific character in a string C#:
string inputString = "programming"; char charToCount = 'r'; int occurrences = inputString.Count(c => c == charToCount); // Result: 2
C# string manipulation count occurrences:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = mainString.Split(new[] { substringToCount }, StringSplitOptions.None).Length - 1; // Result: 4
Substring frequency count in C#:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = Regex.Matches(mainString, Regex.Escape(substringToCount)).Count; // Result: 4
Regex count string occurrences in C#:
string mainString = "Hello, Hello, World, Hello"; string substringToCount = "Hello"; int occurrences = Regex.Matches(mainString, Regex.Escape(substringToCount)).Count; // Result: 4