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

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

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...