Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count of sub-string occurrences? [duplicate]

Tags:

string

c#

Suppose I have a string like:

MyString = "OU=Level3,OU=Level2,OU=Level1,DC=domain,DC=com"; 

then I want to know how many time of occurrences of sub-string "OU=" in this string. With single char, maybe there is something like:

int count = MyString.Split("OU=").Length - 1; 

but Split only works for char, not string.

Also how to find the position of n occurrences? For example, the position of 2nd "OU=" in the string?

How to resolve this issue?

like image 293
KentZhou Avatar asked Mar 22 '13 18:03

KentZhou


People also ask

How many times S2 occurs in S1?

Since the string S2 occurs twice as a non-overlapping subsequence in S1, the required output is 2.


1 Answers

Regex.Matches(input, "OU=").Count 
like image 140
tnw Avatar answered Sep 30 '22 09:09

tnw