c# - Count of tokens in string -


i wondering why following code returns 1 instead of 2. clue? in advance.

string report = "foo bar foo aloha hole hole foo cat gag weird gag strange tourist"; string name = "hole";  int count = regex.matches(report, @"(^|\s)" + regex.escape(name) + @"(\s|$)").count; console.writeline("count " + c); 

if you're trying learn regex, cool, disregard this.

otherwise, regex overkill that's simple using other methods (like linq):

var count = report.split().count(x => x == name); 

Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

javascript - Tooltipster plugin not firing jquery function when button or any click even occur -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -