C # is the fastest way to compare two strings using wildcards

Is there a quickest way to compare two strings (using 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" compared to "Sentence" will return true. (These two comparisons should be the same length)

+4
source share
7 answers

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

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

Assuming matching characters are more likely to match the pattern. Of course, this is just a theory, I would not believe that this changed the situation without comparing it.

And as others pointed out, the subroutine crashes if the mask and line do not have the same length.

+2
source

If mask.length is less than word.length, this function will stop comparing at the end of the mask. Comparing the length of the word / mask at the beginning will prevent this, and also quickly eliminate some obvious inconsistencies.

+3
source

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

Did you profile this code and find that this is a bottleneck in your application? I think this should be good for most purposes.

+2
source

If you used . instead , you can perform a simple regular expression.

+1
source

Variable Length Comparison: I used your code as a source location for my own application, which assumes the mask length is shorter or equal to the length of the comparison text. allowing the use of variable length wildcards in a mask. i.e.: "concat" will match the mask "c ncat" or "ct" 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; } 
+1
source

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

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

I think you are doing a little injustice without giving your code a little context. Of course, if you want to search only one line of characters of the same length as your pattern, then yes, that's fine.

However, if you use this as the basis of a template in which there are several other templates that you will be looking for, this is a bad method. There are other well-known methods, the best of which depend on your specific application. The phrase "inaccurate pattern matching" is the phrase you're worried about.

0
source

Source: https://habr.com/ru/post/1300940/


All Articles