I need your help,is there a ready method which gives you the repetition of word in a string in c#?
Asked
Active
Viewed 93 times
-5
-
1possible duplicate of [Number of occurrences of a character in a string](http://stackoverflow.com/questions/10391481/number-of-occurrences-of-a-character-in-a-string) – Abdul Ahmad May 02 '15 at 17:14
-
Is that what the question is asking? I understood is as "is there a method which repeats a word". – EyasSH May 02 '15 at 17:16
-
do a simple google search @Mohammad come on now there are so many examples of this on the internet for example http://www.dotnetperls.com/string-occurrence – MethodMan May 02 '15 at 17:31
-
Obviously __not a duplicate__ OP is not asking about characters or strings but about word.. – TaW May 02 '15 at 17:35
1 Answers
6
A simple regex will do it:
string str = "abcabcabcabcabc"; // 5 times
int cnt = Regex.Matches(str ,"abc").Count; // 5

Amir Popovich
- 29,350
- 9
- 53
- 99
-
1
-
1
-
See here for a [correct Regex](http://stackoverflow.com/questions/1209049/c-regex-match-whole-words) – TaW May 02 '15 at 17:39