比较c#中的两个字符串是否相等,InvariantCulture和Ordinal比较之间有什么区别?
在C#中比较字符串非常简单.事实上,有几种方法可以做到这一点.我在下面的块中列出了一些.我很好奇的是它们之间的差异以及何时应该使用其他的?是否应该不惜一切代价避免?还有更多我没有列出?
string testString = "Test";
string anotherString = "Another";
if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
Run Code Online (Sandbox Code Playgroud)
(注意:我在这个例子中寻找平等,不小于或大于,但也可以随意发表评论)