Click here to Skip to main content
11,488,877 members (79,709 online)
Click here to Skip to main content

Tagged as

Use wildcard characters * and ? to compare strings

, 6 May 2010 CPOL 30.9K 7
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

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