-1

我有一个称为“单词”的字符串数组。我的数组由一堆模式中的字符串组成,其中模式的第一个字符串是“\”Parameters Table\“”。我需要编写一个代码来搜索 "\"Parameters Table\"" 的单词,然后对其执行一个方法 (data.SortDataMethod()) 直到它再次识别 "\"Parameters Table\"" (其中模式重复),在这种情况下,它将重复相同的方法,直到下一个“\”参数表\“”,依此类推,直到到达文件末尾。我不确定要使用哪个循环或如何从某个点/索引开始读取数组。帮助。谢谢,麻烦您了。

4

2 回答 2

0

这是你要找的吗?

bool found = false;
for (int i = 0; i < words.Length; i++)
{
    if (words[i] == "\"Parameters Table\"")
    {
        found = !found;
    }
    if(found)
    {
        myMethod(words[i]);
    }

}
于 2013-05-01T20:54:03.240 回答
0

代码:

const string separator = "\"Parameteres Table\"";

// Get to the first separator
var cuttedWords = words.SkipWhile(x => x != separator).Skip(1);

// Run as long as there is anything left to scan
while (cuttedWords.Any())
{
    // Take data from the last found separator until the next one, exclusively
    var data = cuttedWords.TakeWhile(x => x != separator).ToArray();

    // Step through cuttedWords to update where the last found separator was
    cuttedWords = cuttedWords.Skip(data.Length + 1);

    // Do what you want with the sub-array containing information
    performWork(data);
}

例子:

var words = new[] {"0", "1", "~", "2", "3", "~", "4", "5", "~", "6", "7", "~", "8"};

var cuttedWords = words.SkipWhile(x => x != "~").Skip(1);

while (cuttedWords.Any())
{
    var data = cuttedWords.TakeWhile(x => x != "~").ToArray();
    cuttedWords = cuttedWords.Skip(data.Length + 1);
    Console.WriteLine(string.Join(",", data));
}

输出:

2,3
4,5
6,7
8
于 2013-05-01T21:13:39.487 回答