如何在列表中查找字符串的索引

Sim*_*lor 19 c# string indexing list

所以我正在尝试做的是检索列表中第一个项目的索引,该索引以"what"开头,我不知道如何做到这一点.

我的尝试(笑):

List<string> txtLines = new List<string>();
//Fill a List<string> with the lines from the txt file.
foreach(string str in File.ReadAllLines(fileName)) {
  txtLines.Add(str);
}
//Insert the line you want to add last under the tag 'item1'.
int index = 1;
index = txtLines.IndexOf(npcID);
Run Code Online (Sandbox Code Playgroud)

是的,我知道这不是什么,它是错的,因为它似乎正在寻找一个等于npcID的项目而不是以它开头的行.

sa_*_*213 50

如果你想要"StartsWith",你可以使用 FindIndex

 int index = txtLines.FindIndex(x => x.StartsWith("whatever"));
Run Code Online (Sandbox Code Playgroud)