1

我正在编写我的第一个程序,我发现它运行缓慢。它用于将一种文本格式转换为另一种格式。这样做时,我经常需要搜索某个字符串,然后从那里解析文本。这可能看起来像:

const string separator = "Parameters Table";

 // Get to the first separator
var cuttedWords = backgroundWords.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 variable = cuttedWords.TakeWhile(x => x != separator).ToArray();

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

    // Do what you want with the sub-array containing information


} 

我想知道是否有更有效的方法来做同样的事情(即搜索一个字符串并使用该字符串和下一个相同字符串之间的子数组执行我想要的操作)。谢谢你。

4

3 回答 3

1

更直接的东西怎么样:

var startIndex = backgroundWords.IndexOf(separator) + separator.Length;
var endIndex = backgroundWords.IndexOf(separator, startIndex);
var cuttedWords = backgroundWords.Substring(startIndex, endIndex);

你可以继续这样剪。当你想前进时,你可以这样做:

// note I added the endIndex + separator.Length variable here to say
// continue with the end of what I found before
startIndex = backgroundWords.IndexOf(separator, endIndex + separator.Length) + separator.Length;
endIndex = backgroundWords.IndexOf(separator, startIndex);
cuttedWords = backgroundWords.Substring(startIndex, endIndex);

因此,您修改后的代码片段将如下所示:

const string separator = "Parameters Table";

var startIndex = backgroundWords.IndexOf(separator) + separator.Length;
var endIndex = backgroundWords.IndexOf(separator, startIndex);
var cuttedWords = backgroundWords.Substring(startIndex, endIndex);

while (cuttedWords.Any())
{
    // Take data from the last found separator until the next one, exclusively
    var variable = cuttedWords.TakeWhile(x => x != separator).ToArray();

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

    startIndex = backgroundWords.IndexOf(separator, endIndex + separator.Length) + separator.Length;
    endIndex = backgroundWords.IndexOf(separator, startIndex);
    cuttedWords = backgroundWords.Substring(startIndex, endIndex);
}
于 2013-06-10T14:01:42.867 回答
1

最简单的方法是拆分字符串。

const string separator = "Parameters Table";
var text = "ThisParameters TableisParameters TableaParameters Tabletest";
var split = text.Split(new[] { separator }, StringSplitOptions.None);
foreach(var x in split)
{
    // do something
}

有没有理由这不起作用?

于 2013-06-10T14:25:52.253 回答
0

在进入循环之前将其转换为列表。否则每次都会重复 Linq 内部工作。

你可以这样尝试:

var lst = cuttedWords.ToList();
while (lst.count() > 0)
{
    // Take data from the last found separator until the next one, exclusively
    var variable = lst.TakeWhile(x => x != separator).ToArray();

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

    // Do what you want with the sub-array containing information


} 
于 2013-06-10T14:14:32.293 回答