【问题标题】:Get all string between a string separator [closed]获取字符串分隔符之间的所有字符串[关闭]
【发布时间】:2018-02-09 15:15:17
【问题描述】:

我需要一个接受字符串的 c# 函数:

输入:"str1__value__xyz__str4__"

我想将其转换为字符串数组(或列表)

输出:string[] output = { str1, value, xyz, str4}

我认为我们可以使用 Linq 或正则表达式。

【问题讨论】:

  • string[] result = source.Split(new string[] { "__"}, StringSplitOptions.RemoveEmptyEntries);
  • RegexLinq 解决方案:string[] result = Regex.Matches(source, "[^_]+").OfType<Match>().Select(match => match.Value).ToArray();
  • 为什么要使用正则表达式?无需复杂化
  • 我投反对票的原因是没有证据表明您已经研究过自己的解决方案或自己尝试过任何事情。

标签: c# regex linq


【解决方案1】:
Input.Split(new string[] { "__" }, StringSplitOptions.None);

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 2018-10-13
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多