C# 连续搜索字符串以查找分隔符?

C# 连续搜索字符串以查找分隔符?,c#,.net,string,C#,.net,String,我正在写我的第一个程序,我发现它运行得很慢。它用于将一种文本格式转换为另一种文本格式。在这样做的过程中,我经常需要搜索某个字符串,然后从那里解析文本。这可能看起来像: const string separator = "Parameters Table"; // Get to the first separator var cuttedWords = backgroundWords.SkipWhile(x => x != separator).Skip(1); // Ru

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

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


} 

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

更直接一些的,比如:

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);
}
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


} 

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

您可以这样尝试:

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);
}
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


} 

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

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
}
这有什么不起作用的原因吗