【发布时间】:2013-05-08 05:32:39
【问题描述】:
所以我要做的是检索列表中以“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 的项目,而不是以它开头的行。
【问题讨论】:
-
它确实显示了字符串的索引。查看dotnetperls.com/indexof
-
到底有什么问题?如果
txtLines是一个文本框,只需执行txtLines.Text.IndexOf(ncpID)。 -
抱歉刚刚修好了,由于某种原因代码没有粘贴。哦,修好了