有一个在运行时添加字符串的列表。 列表中的字符串可以是这样的
List<string> myList=new List<string>();
0 7- 9
1 3 - 6
2 1 -3
3 10-12
这里列表中包含的字符串没有相同的模式。 假设我想找到 3 - 6 的索引。 所以我使用了一个表达式
3\s*\-\s*6
现在如何在 Array.Indexof 方法中使用它,以便我可以从 mylist 中获取该元素的索引。
最佳答案
尝试
myList.FindIndex(s => new Regex(@"3\s*\-\s*6").Match(s).Success);
编辑: 工作示例:
List<string> myList = new List<string>
{
"7- 9",
"3 - 6",
"1 -3",
"10-12"
};
int index = myList.FindIndex(s => new Regex(@"3\s*\-\s*6").Match(s).Success);
Console.WriteLine(index); // 1
关于c# - 在c#.net中的Array.indexof中使用正则表达式查找元素的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758262/