Click here to Skip to main content
11,436,156 members (63,594 online)
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

 
QuestionI broke it [modified] Pin
Roman Shchekin9-Oct-14 4:11
memberRoman Shchekin9-Oct-14 4:11 
GeneralReason for my vote of 5 nice article. Pin
nikhi _singh21-Feb-12 2:40
membernikhi _singh21-Feb-12 2:40 
GeneralReason for my vote of 5 Nice Algorithm. I modified the sourc... Pin
Dirk Moshage18-Jan-12 6:34
memberDirk Moshage18-Jan-12 6:34 
Question[My vote of 1] my vote of 1: why not use standard regex? Pin
SledgeHammer019-Feb-10 10:28
memberSledgeHammer019-Feb-10 10:28 
Why wouldn't you use standard regex for this?

I get that you might have a requirement to support 1980's style DOS wildcards, but those can easily be converted to regex internally.

Also, if you are using this for something like finding files, the OS already supports wildcards, so why re-invent the wheel?
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
PrzemekBenz9-Feb-10 11:07
memberPrzemekBenz9-Feb-10 11:07 
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
Michael Lee Yohe16-Feb-10 13:47
memberMichael Lee Yohe16-Feb-10 13:47 
AnswerRe: [My vote of 1] my vote of 1: why not use standard regex? Pin
PIEBALDconsult20-Feb-12 13:03
memberPIEBALDconsult20-Feb-12 13:03 

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
Web02 | 2.8.150428.2 | Last Updated 6 May 2010
Article Copyright 2010 by PrzemekBenz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid