【问题标题】:C# get specific string from between two string into list<string>C# 从两个字符串之间获取特定字符串到 list<string>
【发布时间】:2017-02-10 15:20:18
【问题描述】:

我有一些关于“从两个字符串之间查找特定字符串并进入列表”的问题。

我有一个这样的字符串:

-begin- 
code:[264]
Name:[1] 
Name:[2] 
Name:[3]
-end- 
code:[264]
Name:[1] 
Name:[4] 
code:[264]
Name:[6]
-begin- 
Name:[6] 
code:[264]
Name:[7] 
Name:[8] 
Name:[1]
-end-

我想将“-begin-”到“-end-”之间的字符串“Name:”拆分为如下所示的列表,

list<1>
Name:[1] 
Name:[2] 
Name:[3]
list<2>
Name:[6] 
Name:[7] 
Name:[8]
Name:[1]

现在我只能将“-begin-”到“-end-”之间的文本拆分到列表中。

int first = 0;
int last = 0;
int number = 0;
int start = 0;
  do {
      first = text.IndexOf("-begin-", start);
      last = text.IndexOf("-begin-", first + 1);
         if (first >= 0 && last >= 0)
           {
                number = (last - first);
                AVPline.Add(text.Substring(first,number).Trim());
                start = last + 1;
           }
     } while (position > 0);

我不知道在“-begin-”到“-end-”之间拆分文本后拆分字符串“Name:”。

谁能帮帮我。

非常感谢。

【问题讨论】:

  • 如果你有一行 "Name:[3]" 你使用 Split(':')[1] 来获取 [3] 部分。你问的是这个吗?
  • 我们仍然没有足够的信息。例如,"-begin-""-end-" 序列似乎与Name 字段的位置不匹配。一些Name 字段位于开始/结束块内,而另一些则不在。比较随意。如果您的示例数据是准确的,那么您只需坐下来自己编写一些详细的解析逻辑 - 没有我们可以帮助您解决的技术问题(解析逻辑除外)。

标签: c# string list substring indexof


【解决方案1】:

String.Split 会将字符串解析为数组子字符串。然后处理子字符串。

string[] parseStr = text.Split('\n');

【讨论】:

  • 非常感谢 jim31415。您的代码很容易将字符串拆分到每一行。
【解决方案2】:

您可以使用枚举器和yield returns 实现更简洁和可维护的代码:

首先,让签名正确。您需要从输入字符串创建列表集合:public static IEnumerable&lt;IList&lt;string&gt;&gt; ProcessText(string input) 看起来不错。

现在,让我们的生活更轻松。首先,让我们将输入字符串分成几行:

var lines = input.Split(new[] { Environment.NewLine });

现在,让我们遍历这些行,但不要使用foreach 来执行此操作。让我们稍微脏一点,直接使用枚举器,原因稍后会变得清楚:

using (var enumerator = lines.GetEnumerator())
{
    while (enumerator.MoveNext()
    {
    }
}

好的,现在我们只需要找到每个子列表的开头并相应地构建它。很简单:

while (enumerator.MoveNext()
{
    if (enumerator.Current == "-begin-"
        yield return createSubList(enumerator).ToList();
}

现在你明白我为什么直接使用枚举器了。这样我可以轻松地调用另一个方法来跟踪我们在输入文本中的位置:

static IEnumerable<string> createSubList(IEnumerator<string> enumerator)
{
    while (enumerator.MoveNext()
    {
        if (enumerator.Current == "-end-")
        {
            yield break;
        }

        if (!enumerator.Current.StartsWith(...whatever strings you want to ignore))
        {
            yield return enumerator.Current;
        }      
    }
}

我们完成了!让我们把它们放在一起:

public IEnumerable<IList<string>> ProcessText(string input)
{
    var lines = input.Split(new[] { Environment.NewLine });

    using (var enumerator = lines.GetEnumerator())
    {
        while (enumerator.MoveNext()
        {
            if (enumerator.Current == "-begin-"
                yield return createSubList(enumerator).ToList();
        }
    }
}

private static IEnumerable<string> createSubList(IEnumerator<string> enumerator)
{
    while (enumerator.MoveNext()
    {
        if (enumerator.Current == "-end-")
        {
            yield break;
        }

        if (!enumerator.Current.StartsWith(...whatever strings you want to ignore))
        {
            yield return enumerator.Current;
        }      
    }
}

【讨论】:

  • 直接用枚举器很好用!为此 +1!
  • 仅当输入字符串包含 NewLine 时才有效,正如我们在 OP 的示例文本中所假设的那样。您的解决方案看起来不错,但恐怕它对 OP 来说太复杂了。
  • 非常感谢 InBetween,使用您的代码后,我可以通过程序的这一部分,但我有更多问题。如果我想在行之间查找字符串,我不能使用 enumerator.Current.StartsWith 我可以使用一些东西喜欢enumerator.Current.Indexof吗?
  • @JooHwang enumerator.Current 是一个string,因此您可以使用您在string 中使用的任何方法或扩展方法。所以,是的,你可以打电话给IndexOf
【解决方案3】:

您的尝试与可行的解决方案相距不远。
我手头只有手机,所以我不能给你测试过的代码。但这是我的建议:

  • 您似乎想要一个字符串列表数组。创建一个字符串列表列表,因为你不知道你的数组会变成多大。最后,您可以轻松地从该列表中获取一个数组。
  • 您需要一个循环。你已经有了。
  • 在循环中,找到你的开始和结束。你几乎已经做到了。您应该搜索 -begin--end-
  • -begin--end- 之间获取所需数据并在内部(嵌套)循环中对其进行处理

嵌套循环:

  • 创建一个字符串列表
  • 获取数据,搜索开始Name和结束],方法与外循环相同。
  • 将找到的元素添加到列表中。

嵌套循环之后,仍然在外循环:

  • 将刚刚填写的字符串列表添加到字符串列表中。

外循环结束。

请记住为您的外循环使用有效条件。在您的示例中,您从未设置过“位置”。

【讨论】:

  • 非常感谢您的概念非常清晰易懂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 2016-06-20
  • 2013-11-25
  • 2012-12-28
  • 2022-12-13
相关资源
最近更新 更多