所以我正在尝试做的是检索列表中以“whatever”开头的第一项的索引,我不确定该怎么做。
我的尝试(笑):
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);
是的,我知道它实际上什么都不是,而且它是错误的,因为它似乎在寻找一个等于 npcID 的项目,而不是以它开头的行。
最佳答案
如果你想要“StartsWith”,你可以使用FindIndex
int index = txtLines.FindIndex(x => x.StartsWith("whatever"));
关于c# - 如何在列表中查找字符串的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16534657/