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

c++11 - Intel compiler and "cannot have an in-class initializer" when using constexpr -

rest - Spring boot: Request method 'PUT' not supported -

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -