gpt4 book ai didi

c# - 检查列表中是否存在不区分大小写的字符串

转载 作者:行者123 更新时间:2023-11-30 13:30:37 25 4
gpt4 key购买 nike

我的列表有误。让我告诉你我的想法:我有一个循环,当完成循环 1 时,值将添加到列表中,并在完成循环时继续。最后,我将在每个循环中包含一个包含所有值的列表。

然后,我想检查列表中是否存在值。如果存在,我会做点什么。

示例:

Loop 1: List: A

Loop 2: List: A,B

Loop 3: List: A,B,A

因为值A是Exist in List。然后,如果 A 存在于 List 中,我将做一些事情

List<string> list = new List<string>();
foreach (DataRow r in dt.Rows)
{
string Url = r["Url"].ToString();
list.Add(Url);
if (list.Contains(Url, StringComparer.OrdinalIgnoreCase))
{
//dosomething
}
}

但是什么也没有发生。希望你能帮助我改进我的代码。谢谢!!!

最佳答案

这样试试

if(list.Where(o=> string.Equals(Url, o, StringComparison.OrdinalIgnoreCase)).Any())
{
// Exists in the list
}

或者

if(list.FindIndex(o=> string.Equals(Url, o, StringComparison.OrdinalIgnoreCase))>-1){

// Exists in the list
}

关于c# - 检查列表中是否存在不区分大小写的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com