Saturday, December 3, 2011

A trick to find how many times a sub string exists in a Main String


The coolest trick to find how many times a substring exist in a main string is to use RegEx class, we can see following sample code:

        static void Main(string[] args)
        {
            string words = "This is a list of words, with: a bit of punctuation" +
                           "\tand a tab character.";

            string[]split = Regex.Split(words, "is");

            Console.Write("Number of times : {0}", split.Length);

            Console.ReadLine();
        }


Output of above code would be :


No comments:

Post a Comment