Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Which is faster: String.Contains() or Regex.isMatch()? [duplicate]

Tags:

c#

.net

asp.net

Possible Duplicate:
Regex.IsMatch vs string.Contains

Which is faster, preferable and why?

What the difference in mechanisms between two?

I need to search for some values from UserAgent, most of values can be used without wildcards (e.g. if I want to catch cellular phones I search for iPhone instead of *iPhone* wildcards).

like image 865
eugeneK Avatar asked Dec 09 '22 15:12

eugeneK


1 Answers

What is faster

Try measuring. But this is the wrong question, see below.

preferable

If I want to match a fixed string String.Contains does just what I need. If I need to pattern match, then String.Contains is useless.

Comparing the performance of these is irrelevant, they do completely different things. Use the right tool first, and only then if your performance is a problem use profiling to identify hot parts of your code to look at.

like image 87
Richard Avatar answered May 12 '23 04:05

Richard