且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#字符串比较由于其中包含特殊字符的字符串而失败

更新时间:2023-11-05 08:40:10

如果要计数,只需 Count()借助 Linq 即可;要测试字符串是否相等,请使用 String.Equals :

If you want to count, just Count() with a help of Linq; to test if strings are equal use String.Equals:

System.Linq;
...
// do not do Trim() repeatedly
String testValue = myInput.Trim();

int count = myList
  .Count(item => String.Equals(item.Trim(), 
                               testValue,
                               StringComparison.OrdinalIgnoreCase));