>> /// <summary> >> /// Occurs function >> /// </summary> >> /// <param name="source"></param> >> /// <param name="pattern"></param> >> /// <returns></returns> >> public static Int32 Occurs(this String source, char pattern) >> { >> return source.Split(pattern).Length - 1; >> } >> >> public static Int32 Occurs(this String source, String pattern) >> { >> return (source.Length - source.Replace(pattern, "").Length)/pattern.Length; >> }Good point, I guess I'll add these two checks in both and return 0 in these cases.
/// <summary> /// Occurs function /// </summary> /// <param name="source"></param> /// <param name="pattern"></param> /// <returns></returns> public static Int32 Occurs(this String source, char pattern) { if (null==pattern) return 0; return source.Split(pattern).Length - 1; } public static Int32 Occurs(this String source, String pattern) { if (String.IsNullOrEmpty(pattern)) return 0; return (source.Length - source.Replace(pattern, "").Length)/pattern.Length; }