在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)
(注意:我在这个例子中寻找平等,不小于或大于,但也可以随意发表评论)
在C#中进行字符串比较时,执行a之间的区别是什么
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.CurrentCultureIgnoreCase);
Run Code Online (Sandbox Code Playgroud)
和
string test = "testvalue";
test.Equals("TESTVALUE", StringComparison.InvariantCultureIgnoreCase);
Run Code Online (Sandbox Code Playgroud)
......无论如何,包含额外参数是否重要?