tags:

views:

154

answers:

1

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).

+5  A: 

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.

Richard
If Regex performs faster due to better algorithm why should i use Contains? I don't need this for little small search but for a complex one where lowest execution time is urgent
eugeneK
@eugeneK: Regex support complex matching logic (consider how to implement regex matching). It would be hard to make a direct string comparison as slow.
Richard
@Richard, thanks
eugeneK