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:
- int count = Regex.Matches(s.ToUpper(), "THE").Count;
Hope it helped
Thanks!
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

loigie panganoronPosted Mar 14, 2016, 4:14 AM
nice :)
Sonu ChaudharyPosted Feb 25, 2016, 7:14 AM
nice article
Sonu ChaudharyPosted Feb 25, 2016, 7:14 AM
nice article
Akash VarshneyPosted Oct 3, 2015, 11:21 AM
Good one !! Inline Solution :0
pooja guptaPosted Sep 8, 2015, 3:59 AM
anyways nice approach..
pooja guptaPosted Sep 8, 2015, 3:59 AM
hmm, it do but here you are using regex to find the occurence. i guess regex uses loop internally to find occurence for you. So ultomately loop is being used.. I guess indexing will be the nicer approach to found a particular object without using loop