If i got a text file
"dont run if you cant hide, or you will be broken in two strings, your a evil man"
and i want to count how many times the word you is in the text file, and put that value in to a int variable.
how do i go about doing somthing like that?
To say it with a Regex...
Console.WriteLine((new Regex(@"(?i)you")).Matches("dont run if you cant hide, or you will be broken in two strings, your a evil man").Count)
or if you need the word you as stand-alone
Console.WriteLine((new Regex(@"(?i)\byou\b")).Matches("dont run if you cant hide, or you will be broken in two strings, your a evil man").Count)
Edit: Replaced \s+you\s+ with (?i)\byou\b for the sake of correctness
string s = "dont run if you cant hide, or you will be broken in two strings, your a evil man";
var wordCounts = from w in s.Split(' ')
group w by w into g
select new { Word = g.Key, Count = g.Count() };
int youCount = wordCounts.Single(w => w.Word == "you").Count;
Console.WriteLine(youCount);
Ideally punctuation should be ignored. I'll let you handle a messy detail like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With