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
Post a Comment