【问题标题】:String Comparison when list of strings start with "/"字符串列表以“/”开头时的字符串比较
【发布时间】:2021-12-28 18:19:51
【问题描述】:

试图为这段代码返回真或假:

var searchString = "abc";
var list= new List<string>() { "/abc", "/xyz" };
bool found= list.Any(x => searchString.Contains(x, StringComparison.CurrentCultureIgnoreCase));

found 在这种情况下返回 false,但是我需要它从定义的列表 {"/abc", "/xyz"} 中找到“abc”并返回 true。这里有什么问题?

【问题讨论】:

    标签: c# asp.net string-comparison


    【解决方案1】:

    你应该改变比较:

    var searchString = "abc";
    var list= new List<string>() { "/abc", "/xyz" };
    
    bool found = list.Any(x => x.Contains(searchString, StringComparison.CurrentCultureIgnoreCase));
    //                         ^          ^^^^^^^^^^^^
    

    因为“abc”不包含“/abc”,反之亦然。您希望在集合的每个项目中搜索 searchString。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多