简体   繁体   English

进行字符串比较的最有效方法是什么?

[英]What is the most efficient way to do the String comparison?

Both do the same thing.两者都做同样的事情。 What is the most efficient way to compare two strings?比较两个字符串的最有效方法是什么? Equals() or Compare()?等于() 还是比较()? Are there any differences?有什么区别吗?

        if (String.Equals(StringA, StringB, StringComparison.OrdinalIgnoreCase))
        {
            // Your code goes here
        }

        if (String.Compare(StringA, StringB, true) == 0)
        {
            // Your code goes here
        }

Equals() will tell you whether they are equal or not. Equals() 会告诉你它们是否相等。 Compare() will tell you how equal they are. Compare() 会告诉你它们有多平等。 It "returns an integer that indicates their relative position in the sort order".它“返回一个整数,指示它们在排序顺序中的相对位置”。 As for performance, basically the same for almost all purposes.至于性能,几乎所有用途都基本相同。

Why don't you just try it out with a microbenchmark and find out?为什么不直接使用微基准测试并找出答案?
Let each statement run atleast 100000 times.让每个语句至少运行 100000 次。

是诺亚科德的比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM