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!

9 Comments

Join the conversation! Your thoughts help the community grow.

  • Akash Varshney

    Good one !! Inline Solution :0

  • anyways nice approach..

  • 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