【发布时间】:2014-08-06 16:52:01
【问题描述】:
我需要搜索一个字符串数组以查找“参数表”的每一个出现,并在每个“参数表”和下一个“参数表”之间,从指定的索引中获取另一个字符串(保持不变)。我一直在这样做:
public List<string> findlistOfNames(string[] arrayToLookForNames)
{
List<string> listOfNames = new List<string>();
const string separator = "Parameters Table"; //This is the string I am searching for
var cuttedWords = arrayToLookForNames.SkipWhile(x => x != separator).Skip(1);
while (cuttedWords.Any())
{
var variable = cuttedWords.TakeWhile(x => x != separator).ToArray();
cuttedWords = cuttedWords.Skip(variable.Length + 1);
listOfNames.Add(variable[2]); //This (always index 2) needs to be added to the list
}
return listOfNames;
}
这运行非常缓慢。有没有更好的方法来做到这一点?
编辑:这是string[] arrayToLookForNames的sn-p:
参数表
0
41
气压
百帕
AFD2
记录
参数表
0
42
气压设置
在汞
【问题讨论】:
-
看来您的代码 a) 当前有效,b) 您正在寻求改进它。一般来说,这些问题对于这个网站来说太过分了,但你可能会在codereview.SE找到更好的运气。
-
正是如此。我去看看,谢谢。
-
在你发帖之前(如果你这样做的话)至少添加一个你传递给函数的数据示例。
-
在这里使用
Take/Skip/TaksWhile/SkipWhile会使整个事情变得非常低效。您应该改用索引器访问。 -
这个问题似乎离题了,因为它是在codereview.stackexchange.com/questions/54431/…交叉发布的
标签: c# visual-studio-2010 list methods arrays