C# - 这是更快:String.Contains()或Regex.isMatch()?这是、更快、String、isMatch

2023-09-04 12:14:56 作者:少年与猫

可能重复:    Regex.IsMatch VS string.Contains

这是更快,preferable为什么?

什么两者之间的机制有什么区别?

我要寻找从用户代理的一些值,最值,可以不带通配符(如使用,如果我想赶上手机我,而不是搜索 iPhone * iPhone * 通配符)。

解决方案   

什么是快

尝试测量。但是,这是的错误的问题的,见下文。

  

preferable

如果我想匹配一个固定的字符串 String.Contains 不正是我需要的。如果我需要的模式匹配,则 String.Contains 是没用的。

比较这些性能是无关紧要的,他们这样做完全不同的事情。第一次使用正确的工具,也只有这样,如果你的表现是一个问题,使用分析,以确定您的code热部件来看待。

Possible Duplicate: Regex.IsMatch vs string.Contains

C 如何通过foreach 修改Dictionary的value

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

解决方案

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.