C#中比较字符串非常简单。实际上,有几种方法可以做到这一点。我在下面的块中列出了一些。我很好奇它们之间的区别以及在何时应该使用其中之一?是否应该尽可能避免其中一种方法?还有更多我没有列出来的方法吗?
string testString = "Test";
string anotherString = "Another";
if (testString.CompareTo(anotherString) == 0) {}
if (testString.Equals(anotherString)) {}
if (testString == anotherString) {}
(注意:在此示例中,我寻求平等而非小于或大于,但您也可以随意评论)