Count Matches - CSharp System

CSharp examples for System:String Match

Description

Count Matches

Demo Code


using System;//  www .ja v  a2 s  . c om

public class Main{
        public static int CountMatches(string str, string sub)
		{
			if (!String.IsNullOrEmpty(str) && !String.IsNullOrEmpty(sub))
			{
				int count = 0;

				for (int idx = 0; (idx = str.IndexOf(sub, idx, StringComparison.Ordinal)) != -1; idx += sub.Length)
				{
					++count;
				}

				return count;
			}
			return 0;
		}
}

Related Tutorials