Get Count Of A Substring In A String Without Using Loops

Here we will see how a string occurrence can be counted without using loops. Let's start with a sample string:
string s ="This is a sample string and the motive of the same is to get the count of the used in it";  
Now, we would try and find the count of "the" used in this string without any loops.
 
In order to do that, we will use the following reference: System.Text.RegularExpressions
 
The query for getting count the count is shown below:
  1. int count = Regex.Matches(s.ToUpper(), "THE").Count;  
This will give us the count of the occurrence of "the" in the string.
 
Hope it helped
Thanks!
 
Ebook Download
View all
Learn
View all