Cloud Training (Online)




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;