Click here to Skip to main content
Click here to Skip to main content

Tagged as

Use wildcard characters * and ? to compare strings

, 6 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Method to compare Strings with wildcard characters:public Boolean MatchWildcardString(String pattern, String input){ if (String.Compare(pattern, input) == 0) { return true; } else if(String.IsNullOrEmpty(input)) { if...
Method to compare Strings with wildcard characters:
 
public Boolean MatchWildcardString(String pattern, String input)
{
    if (String.Compare(pattern, input) == 0)
    {
        return true;
    }
    else if(String.IsNullOrEmpty(input))
    {
        if (String.IsNullOrEmpty(pattern.Trim(new Char[1] { '*' })))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if(pattern.Length == 0)
    {
        return false;
    }
    else if (pattern[0] == '?')
    {
        return MatchWildcardString(pattern.Substring(1), input.Substring(1));
    }
    else if (pattern[pattern.Length - 1] == '?')
    {
        return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1));
    }
    else if (pattern[0] == '*')
    {
        if (MatchWildcardString(pattern.Substring(1), input))
        {
            return true;
        }
        else
        {
            return MatchWildcardString(pattern, input.Substring(1));
        }
    }
    else if (pattern[pattern.Length - 1] == '*')
    {
        if (MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input))
        {
            return true;
        }
        else
        {
            return MatchWildcardString(pattern, input.Substring(0, input.Length - 1));
        }
    }
    else if (pattern[0] == input[0])
    {
        return MatchWildcardString(pattern.Substring(1), input.Substring(1));
    }
    return false;
}
 
The above method can be tested in the following way:
 
// Positive Tests
Assert.IsTrue(MatchWildcardString("*", ""));
Assert.IsTrue(MatchWildcardString("?", " "));
Assert.IsTrue(MatchWildcardString("*", "a"));
Assert.IsTrue(MatchWildcardString("*", "ab"));
Assert.IsTrue(MatchWildcardString("?", "a"));
Assert.IsTrue(MatchWildcardString("*?", "abc"));
Assert.IsTrue(MatchWildcardString("?*", "abc"));
Assert.IsTrue(MatchWildcardString("*abc", "abc"));
Assert.IsTrue(MatchWildcardString("*abc*", "abc"));
Assert.IsTrue(MatchWildcardString("*a*bc*", "aXXXbc"));
 
// Negative Tests
Assert.IsFalse(MatchWildcardString("*a", ""));
Assert.IsFalse(MatchWildcardString("a*", ""));
Assert.IsFalse(MatchWildcardString("?", ""));
Assert.IsFalse(MatchWildcardString("*b*", "a"));
Assert.IsFalse(MatchWildcardString("b*a", "ab"));
Assert.IsFalse(MatchWildcardString("??", "a"));
Assert.IsFalse(MatchWildcardString("*?", ""));
Assert.IsFalse(MatchWildcardString("??*", "a"));
Assert.IsFalse(MatchWildcardString("*abc", "abX"));
Assert.IsFalse(MatchWildcardString("*abc*", "Xbc"));
Assert.IsFalse(MatchWildcardString("*a*bc*", "ac"));
 
Best Regards,
Przemyslaw Benz

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PrzemekBenz
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 Nice Algorithm. I modified the sourc... PinmemberDirk Moshage18-Jan-12 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 6 May 2010
Article Copyright 2010 by PrzemekBenz
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid