查找包含值的字符串数组中字符串的最后一个索引

时间:2022-10-17 04:17:57


In my program I have a text, which I'm splitting up into an Array of lines to modify the lines separately. For one modification I need to find the last index of a value in the text, to insert a new value in the line below.

在我的程序中,我有一个文本,我将其分成一个行数组来分别修改行。对于一个修改,我需要在文本中找到值的最后一个索引,以在下面的行中插入一个新值。

Example:

例:

text=["I like bananas",
      "I like apples",
      "I like cherries",
      "I like bananas",
      "I like apples"]

value: "bananas"

I already tried to use:

我已经尝试过使用:

int pos = Array.FindIndex(text, row => row.Contains(value));
if (pos > -1)
{
 //insert line at pos
}

but this seems to return me the index of the first occurence. "lastIndexOf()" seems neither to be the right command for me as this looks for equality of the given value and the items in the array.

但这似乎又回到了我第一次出现的指数。 “lastIndexOf()”对我来说似乎不是正确的命令,因为它会查找给定值和数组中项目的相等性。

Am I doing something wrong or is there another command, to use in this case?

我做错了什么或是否有另一个命令,在这种情况下使用?

5 个解决方案

#1


5  

I don't get what is wrong with Array.FindLastIndex() method. It works exactly in the same way:

我没有得到Array.FindLastIndex()方法的错误。它的工作原理完全相同:

string value = "bananas";
string[] arr = 
{
    "I like bananas", 
    "I like apples",
    "I like cherries",
    "I like bananas",
    "I like apples"
};

Console.WriteLine(Array.FindLastIndex(arr, x => x.Contains(value)));

This code works for me and outputs "3".

此代码适用于我并输出“3”。

#2


3  

If you replace your array with List<T> you'll get a nice FindLastIndex() method, which is exactly what you're looking for:

如果用List 替换你的数组,你会得到一个很好的FindLastIndex()方法,这正是你正在寻找的:

var texts = new List<string>(...);
var bananaIndex = texts.FindLastIndex(s => s.Contains("banana"));

#3


0  

You can use a List<T> instead of an Array. This would expose FindLastIndex() method, which will be exact for your need

您可以使用List 而不是Array。这将暴露FindLastIndex()方法,这将完全符合您的需要

#4


0  

With Linq you can Reverse the array.

使用Linq,您可以反转阵列。

int pos = Array.FindIndex(text.Reverse(), row => row.Contains(value));
if (pos > -1)
{
 //insert line at pos
}

Make sure you change the pos though. So it will be compatible with the actual array.Something like: pos = (text.Length-1-pos)

确保你改变了pos。所以它将与实际的数组兼容。例如:pos =(text.Length-1-pos)

Link to documentation

链接到文档

#5


0  

The input is an Array. so first you have to convert it to a List()

输入是一个数组。所以首先你必须将它转换为List()

string[] myArray= new string[]{"I like bananas",
  "I like apples",
  "I like cherries",
  "I like bananas",
  "I like apples"};
var pos = myArray.ToList().FindLastIndex(x => x.Contains("banana"));

Or you can apply

或者你可以申请

var pos =Array.FindLastIndex(myArray,x => x.Contains("banana"));

#1


5  

I don't get what is wrong with Array.FindLastIndex() method. It works exactly in the same way:

我没有得到Array.FindLastIndex()方法的错误。它的工作原理完全相同:

string value = "bananas";
string[] arr = 
{
    "I like bananas", 
    "I like apples",
    "I like cherries",
    "I like bananas",
    "I like apples"
};

Console.WriteLine(Array.FindLastIndex(arr, x => x.Contains(value)));

This code works for me and outputs "3".

此代码适用于我并输出“3”。

#2


3  

If you replace your array with List<T> you'll get a nice FindLastIndex() method, which is exactly what you're looking for:

如果用List 替换你的数组,你会得到一个很好的FindLastIndex()方法,这正是你正在寻找的:

var texts = new List<string>(...);
var bananaIndex = texts.FindLastIndex(s => s.Contains("banana"));

#3


0  

You can use a List<T> instead of an Array. This would expose FindLastIndex() method, which will be exact for your need

您可以使用List 而不是Array。这将暴露FindLastIndex()方法,这将完全符合您的需要

#4


0  

With Linq you can Reverse the array.

使用Linq,您可以反转阵列。

int pos = Array.FindIndex(text.Reverse(), row => row.Contains(value));
if (pos > -1)
{
 //insert line at pos
}

Make sure you change the pos though. So it will be compatible with the actual array.Something like: pos = (text.Length-1-pos)

确保你改变了pos。所以它将与实际的数组兼容。例如:pos =(text.Length-1-pos)

Link to documentation

链接到文档

#5


0  

The input is an Array. so first you have to convert it to a List()

输入是一个数组。所以首先你必须将它转换为List()

string[] myArray= new string[]{"I like bananas",
  "I like apples",
  "I like cherries",
  "I like bananas",
  "I like apples"};
var pos = myArray.ToList().FindLastIndex(x => x.Contains("banana"));

Or you can apply

或者你可以申请

var pos =Array.FindLastIndex(myArray,x => x.Contains("banana"));