简体   繁体   中英

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. Compare() will tell you how equal they are. 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.

是诺亚科德的比较。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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