Here we use a simple way to count the string occurence in a sample string without using for loop.
first of all we use a name space using System.Text.RegularExpressions
then take a sample string here i take this
string s="One reason people lie is to achieve personal power. Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date. However, I didn’t have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date. I didn’t want to be embarrassed, so I claimed that I had a lot of work to do. I said I could easily find a date even better than his if I wanted to. I also told him that his date was ugly. I achieved power to help me feel confident; however, I embarrassed my friend and his date. Although this lie helped me at the time, since then it has made me look down on myself."
Now the code for counting the purticular string
int count=Regex.Matches(s.ToUpper(), "THE").Count;
and now print on console like this
Console.WriteLine(count);
first of all we use a name space using System.Text.RegularExpressions
then take a sample string here i take this
string s="One reason people lie is to achieve personal power. Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date. However, I didn’t have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date. I didn’t want to be embarrassed, so I claimed that I had a lot of work to do. I said I could easily find a date even better than his if I wanted to. I also told him that his date was ugly. I achieved power to help me feel confident; however, I embarrassed my friend and his date. Although this lie helped me at the time, since then it has made me look down on myself."
Now the code for counting the purticular string
int count=Regex.Matches(s.ToUpper(), "THE").Count;
and now print on console like this
Console.WriteLine(count);
No comments:
Post a Comment