I was looking for an answer for "how to count the occurrence of a substring in the desired string in c# using Regex?". There are many references like here to use "Count" as following:
int count = Regex.Match(MyString, "OU=").Count
I tried to use it but every time in c# there was an error message and a suggestion to use ".Length".
Eventually, I did it using the following code snippet.
private int GetCounts(string source, string substring)
{
int iCount = 0;
foreach (Match match in Regex.Matches(source, substring))
iCount++;
return iCount;
}
Could anybody explain why ".Count" is not working as expected? Answers appreciated in advance.