How to Count Occurrences of a string within a string in C#?

This post explains how you can use the split method in C# to count a string’s occurrences with-in a string.

How to Count Occurrences of a String within a string in C#?

Assume that you have a string that contains a value “DeveloperPublish/isenthil/blogger” and you wish you to find the number of occurrences of / with-in the string. Here’s a code snippet showing how you can find it.

string input = "DeveloperPublish/isenthil/blogger";
int count = input.Split("/").Length - 1;

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...