C# 检查列表是否包含某个字符串(在 object.Titel 上)但忽略字母的大小写?
C# check if list contains a certain string (on object.Titel) but ignores the casing of the letters?
这是我的代码:
string zoekstring = Request.QueryString["txt"].Replace("'", "''");
List<IntranetDocument> result = documenten.FindAll(x => x.Titel.Contains(zoekstring));
if(result.Any())
{
foreach (IntranetDocument doc in result)
{
ListItem li = new ListItem();
li.Text = doc.Titel;
li.Value = doc.Locatie;
ListFiles.Items.Add(li);
}
}
else
{
ListItem li = new ListItem();
li.Text = Res.Get("Algemeen_NotFound");
li.Value = "#";
ListFiles.Items.Add(li);
}
这将对我的所有文档(我之前通过 SQL 查询加载的)执行 FindAll 并检查文档标题。因此,如果我搜索 "report",它将返回标题包含 "report" 的所有文档,例如季度财务报告。
现在我还想要文档 returned,其中标题包含带有大写 R 的 "Report"。甚至 "rePort" 或 "ReporT" 或 "REPORT" ......你明白了。我希望 FindAll ... Titel.Contains(zoekstring) 不仅 return 标题包含 LITERALLY "zoekstring" 的文档,而且还有潜在的 uppercase/lowercase...
我怎样才能做到这一点?
使用 String.IndexOf
和 StringComparison.CurrentCultureIgnoreCase
而不是 Contains
:
List<IntranetDocument> result = documenten.FindAll(x => x.Titel.IndexOf(zoekstring, StringComparison.CurrentCultureIgnoreCase) >= 0);
这是我的代码:
string zoekstring = Request.QueryString["txt"].Replace("'", "''");
List<IntranetDocument> result = documenten.FindAll(x => x.Titel.Contains(zoekstring));
if(result.Any())
{
foreach (IntranetDocument doc in result)
{
ListItem li = new ListItem();
li.Text = doc.Titel;
li.Value = doc.Locatie;
ListFiles.Items.Add(li);
}
}
else
{
ListItem li = new ListItem();
li.Text = Res.Get("Algemeen_NotFound");
li.Value = "#";
ListFiles.Items.Add(li);
}
这将对我的所有文档(我之前通过 SQL 查询加载的)执行 FindAll 并检查文档标题。因此,如果我搜索 "report",它将返回标题包含 "report" 的所有文档,例如季度财务报告。
现在我还想要文档 returned,其中标题包含带有大写 R 的 "Report"。甚至 "rePort" 或 "ReporT" 或 "REPORT" ......你明白了。我希望 FindAll ... Titel.Contains(zoekstring) 不仅 return 标题包含 LITERALLY "zoekstring" 的文档,而且还有潜在的 uppercase/lowercase...
我怎样才能做到这一点?
使用 String.IndexOf
和 StringComparison.CurrentCultureIgnoreCase
而不是 Contains
:
List<IntranetDocument> result = documenten.FindAll(x => x.Titel.IndexOf(zoekstring, StringComparison.CurrentCultureIgnoreCase) >= 0);