ExtensionMethod.NET Home of 877 C#, Visual Basic, F# and Javascript extension methods


Determines whether a string has multiple occurrences of a particular character. May be helpful when parsing file names, or ensuring a particular string has already contains a given character. This may be extended to use strings, rather than a char.


public static partial class StringExtensions
    public static bool HasMultipleInstancesOf(this string input, char charToFind)

        if ((string.IsNullOrEmpty(input)) || (input.Length == 0) || (input.IndexOf(charToFind) == 0))
            return false;

        if (input.IndexOf(charToFind) != input.LastIndexOf(charToFind))
            return true;

        return false;


string myStr1= "Hello.World.txt";
string myStr2= "Hello World";

bool hasMultPeriods = myStr1.HasMultipleInstancesOf('.'); //returns true
bool hasMultSpaces = myStr2.HasMultipleInstancesOf(' '); //returns false

Author: Phil Campbell

Submitted on: 9 nov. 2009

Language: C#

Type: System.String

Views: 4562