连续搜索字符串以找到分隔符?

时间:2013-06-10 13:29:30

标签: c# .net string

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

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


} 

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

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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
}

有没有理由这不起作用?

答案 2 :(得分: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


}