0

Possible Duplicate:
C#: finding instances of a string within a string

I want to get count of the 'if' keywords in an input , i have wrote this code,but not working at all

string pattern = "if" + "\\B";

also

string pattern = "\\B" + "if" + "\\B";

What pattern should am I use ?

Community
  • 1
  • 1
sap
  • 87
  • 1
  • 11
  • Define "not working at all" - what _does_ it do? What is different from your expectation? Why did you choose this pattern? – Oded Feb 17 '12 at 19:53
  • This could be useful: http://stackoverflow.com/questions/541954/how-would-you-count-occurences-of-a-string-within-a-string-c – ToddBFisher Feb 17 '12 at 19:54

1 Answers1

0

Maybe this...

string sentence = "if abc if xyz"
string keyword = "if";
string pattern = @"\b" + Regex.Escape(keyword) + @"\b";
int count = Regex.Matches(sentence, pattern).Count

Hope it helps.

MNIK
  • 1,581
  • 3
  • 16
  • 22