Feedback

Vorkommen eines Strings in einem anderen zählen

Sprache: C#

Hiermit kann man nicht nur die Anzahl des Vorkommens eines einzelnen Zeichens auslesen, was so funktioniert: [code]int count = myString.Count(c => c == 'e');[/code] Sondern das gleiche für Strings. Dies mache ich über SplitString.
/// <summary>
/// Counts how many times a strings appears in an other.
/// </summary>
/// <param name="input">The String which contains the other.</param>
/// <param name="sub">The string which should be counted.</param>
/// <returns>The count of the string as integer.</returns>
private int CountOf(string input, string substring)
{
    string[] seperator = { substring };
    int count = 0;
    try
    {
        count = input.Split(seperator, StringSplitOptions.None).Count() - 1;
    }
    catch
    {
    }
    return count;
}
/// <summary>
/// Counts how many times a strings appears in an other.
/// </summary>
/// <param name="input">The String which contains the other.</param>
/// <param name="sub">The string which should be counted.</param>
/// <returns>The count of the string as integer.</returns>
private int CountOf(string input, string substring)
{
    string[] seperator = { substring };
    int count = 0;
    try
    {
        count = input.Split(seperator, StringSplitOptions.None).Count() - 1;
    }
    catch
    {
    }
    return count;
}