获取数组中特定项的索引

时间:2022-03-20 16:43:57

i want to retrieve the index of an array but i know only a part of the actual value in the array for example i am storing author name in the array dynamically say "author = 'xyz' " i want to find the index of array item containing something like author as i don't know the value part how to do this.

我想检索索引数组,但我只知道一个数组中的实际价值的一部分,例如我将作者姓名存储在数组动态说“作者= xyz”我想找到数组的索引项包含类似作者作为值部分我不知道如何做到这一点。

4 个解决方案

#1


88  

You can use FindIndex

您可以使用FindIndex

 var index = Array.FindIndex(myArray, row => row.Author == "xyz");

Edit: I see you have an array of string, you can use any code to match, here an example with a simple contains:

编辑:我看到你有一个字符串数组,你可以使用任何代码来匹配,这里有一个简单的包含:

 var index = Array.FindIndex(myArray, row => row.Contains("Author='xyz'"));

Maybe you need to match using a regular expression?

也许您需要使用正则表达式进行匹配?

#2


9  

try Array.FindIndex(myArray, x => x.Contains("author");

尝试数组。FindIndex(myArray x = > x.Contains(“作者”);

#3


6  

The previous answers will only work if you know the exact value you are searching for - the question states that only a partial value is known.

之前的答案只有当你知道你正在搜索的确切值时才会起作用——问题是只知道一个部分的值。

Array.FindIndex(authors, author => author.Contains("xyz"));

This will return the index of the first item containing "xyz".

这将返回包含“xyz”的第一项的索引。

#4


6  

     int i=  Array.IndexOf(temp1,  temp1.Where(x=>x.Contains("abc")).FirstOrDefault());

#1


88  

You can use FindIndex

您可以使用FindIndex

 var index = Array.FindIndex(myArray, row => row.Author == "xyz");

Edit: I see you have an array of string, you can use any code to match, here an example with a simple contains:

编辑:我看到你有一个字符串数组,你可以使用任何代码来匹配,这里有一个简单的包含:

 var index = Array.FindIndex(myArray, row => row.Contains("Author='xyz'"));

Maybe you need to match using a regular expression?

也许您需要使用正则表达式进行匹配?

#2


9  

try Array.FindIndex(myArray, x => x.Contains("author");

尝试数组。FindIndex(myArray x = > x.Contains(“作者”);

#3


6  

The previous answers will only work if you know the exact value you are searching for - the question states that only a partial value is known.

之前的答案只有当你知道你正在搜索的确切值时才会起作用——问题是只知道一个部分的值。

Array.FindIndex(authors, author => author.Contains("xyz"));

This will return the index of the first item containing "xyz".

这将返回包含“xyz”的第一项的索引。

#4


6  

     int i=  Array.IndexOf(temp1,  temp1.Where(x=>x.Contains("abc")).FirstOrDefault());