Languages
[Edit]
EN

C# / .NET - count character occurrences

0 points
Created by:
Pearl-Hurley
559

In this article, we would like to show you how to count occurrences of a character in a String in C#.

Quick solution:

Output:

 

1. Using simple for loop

Edit

In this example, we create simple for loop to iterate through the string and check if each character matches the character we want.

Output:

2. Using String.Replace()

Edit

In this example, to count character occurrences, we calculate the difference between the original string and the string with removed specified characters.

Output:

3. Using String.Split()

Edit

In this example, we use Split() method with string Length to count the occurrences of the A character in the text.

Output:

References

Edit
  1. String.Replace Method (System) | Microsoft Docs
  2. String.Split Method (System) | Microsoft Docs

Alternative titles

  1. C# / .NET - count character occurrences in string
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join