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

标签 c# string list indexing

所以我正在尝试做的是检索列表中以“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/

相关文章:

创建fdf的python脚本

python - 无法对我的列表进行排序,因为它是 NoneType?简单的 python

Python:比较列表的语义

c# - 如何检查一个项目是否等于数组中的任何项目?

c# - 将 Windows-1252 字符串与 UTF-8 字符串进行比较

c# - 在服务中使用 A3 证书

c - 反转字符串的某些部分时遇到问题

python - 将作为另一个列表元素的列表元素从字符串转换为整数

c# - 一次性密码 (OTP) C# 到 Java 的代码转换

c# - 我应该避免在 winforms 的后台线程上做什么