This question has already been answered:

Suppose there is a .txt document with the text:

 "У меня есть собака. Моя собака самая лучшая." 

Need to find out the number of words "собака" (there may be more than 50 pieces).

Reported as a duplicate by participants free_ze , 0xdb , Suvitruf , EvgeniyZ , ߊߚߤߘ Apr 29 '18 at 11:14 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 3
    What were you trying to do yourself? And then the homework for our students do not decide here. - VladD 2:55 pm

3 answers 3

 string search = "Прицепом прицеплен прицеп"; string pattern = $"\\b{Regex.Escape(search)}\\b"; int count = new Regex(pattern, RegexOptions.IgnoreCase).Matches(source).Count; 
     String s = "У меня есть собака. Моя собака самая лучшая."; Regex.Matches(s, "собака").Count // выведет 2 
    • 3
      And if you do not need to look for the word "Dog", but say the word "trailer" in the proposal, the trailer has a trailer attached, what will your method return? - Digital Core
    • 2 or 3 if used with .lower () - nick_gabpe
    • No, you did not catch the point, you have the wrong method. You need to search for a target word, not an entry! - Digital Core
     "У меня есть собака. Cобака моя самая лучшая надсобака из всех подсобак." .Split(',','.',' ',';') .Select(s => s.Trim().ToLower()) .Count(s => s == "собака".ToLower())