gpt4 book ai didi

c# - 使用参数 c# 查找列表的索引

转载 作者:行者123 更新时间:2023-11-30 15:31:35 24 4
gpt4 key购买 nike

我基本上是在尝试转换它:

private int FindIndexOf(string valueOne, string valueTwo, List<string> list)
{
return list.FindIndex(s =>
s.ToLower().Contains(valueOne.ToLower()) &&
s.ToLower().Contains(valueTwo.ToLower())
);
}

像这样使用参数:

private int FindIndexOf(List<string> list, params string[] args)
{
string pattern = String.Format("([{0}]+)", String.Join("]+[", args));
Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase);
return list.FindIndex(s => regEx.IsMatch(s.ToLower()));
}

我不太擅长 RegEx,我想不出在 linq 中实现它的方法。有什么建议吗?

最佳答案

您不需要正则表达式,也不需要 ToLower:

private int FindIndexOf(List<string> list, params string[] args)
{
return list
.FindIndex(s => args
.All(str => s.IndexOf(str, StringComparison.OrdinalIgnoreCase) >= 0));
}

为什么 ToLower 可能不正确:https://stackoverflow.com/a/234751/284240

关于c# - 使用参数 c# 查找列表的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20435479/

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