Gregoire
Gregoire

Reputation: 24832

C# - fastest way to compare two strings using wildcards

Is there a fastest way to compare two strings (using the space for a wildcard) than this function?

public static bool CustomCompare(this string word, string mask)
{

    for (int index = 0; index < mask.Length; index++)
    {
        if (mask[index] != ' ') && (mask[index]!= word[index]))
        {
            return false;
        }
    }
    return true;
}

Example: "S nt nce" comparing with "Sentence" will return true. (The two being compared would need to be the same length)

Upvotes: 4

Views: 6113

Answers (7)

sparkplug
sparkplug

Reputation: 1366

Not sure if this is any faster but it looks neat:

public static bool CustomCompare(this string word, string mask)
{
     return !mask.Where((c, index) => c != word[index] && c != ' ').Any();
}

Upvotes: 1

MattC
MattC

Reputation: 134

Variable Length comparison: I used your code as a starting place for my own application which assumes the mask length is shorter or equal to the comparison text length. allowing for a variable length wildcard spot in the mask. ie: "concat" would match a mask of "c ncat" or "c t" or even "c nc t"

    private bool CustomCompare(string word, string mask)
    {
        int lengthDifference = word.Length - mask.Length;
        int wordOffset = 0;
        for (int index = 0; index < mask.Length; index++)
        {
            if ((mask[index] != ' ') && (mask[index]!= word[index+wordOffset]))                
            {
                if (lengthDifference <= 0)
                {
                    return false;
                }
                else
                {
                    lengthDifference += -1;
                    wordOffset += 1;
                }
            }
        }
        return true;
    } 

Upvotes: 1

user124493
user124493

Reputation:

I think you're doing a little injustice by not giving a little bit of context to your code. Sure, if you want to search only one string of characters of the same length as your pattern, then yes this is fine.

However, if you are using this as the heart of a pattern matcher where there are several other patterns you will be looking for, this is a poor method. There are other known methods, the best of which depends on your exact application. The phrase "inexact pattern matching" is the phrase you are concerned with.

Upvotes: 0

Kevin Gale
Kevin Gale

Reputation: 4438

The loop is pretty simple and I'm not sure you can do much better. You might be able to micro optimize the order of the expression in the if statement. For example due to short circuiting of the && it might be faster to order the if statement this way

 if (mask[index]!= word[index])) && (mask[index] != ' ')

Assuming that matching characters is more common that matching the wildcard. Of course this is just theory I wouldn't believe it made a difference without benchmarking it.

And as others have pointed out the routine fails if the mask and string are not the same length.

Upvotes: 2

Rob Elliott
Rob Elliott

Reputation: 2008

If mask.length is less than word.length, this function will stop comparing at the end of mask. A word/mask length compare in the beginning would prevent that, also it would quick-eliminate some obvious mismatches.

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351506

That looks like a pretty good implementation - I don't think you will get much faster than that.

Have you profiled this code and found it to be a bottleneck in your application? I think this should be fine for most purposes.

Upvotes: 2

Anon.
Anon.

Reputation: 59973

If you used . instead of , you could do a simple regex match.

Upvotes: 1

Related Questions