C#在长字符串上每隔一个逗号分割字符串

时间:2018-12-15 23:44:53

标签: c# .net string split

我有一个看起来像这样的字符串:

string  a =  "Stuff, another thing, random stuff, snuff, Pigs are wierd, sick, Cats are dangerous, they will kill you, Cows produce milk, but horses don't"

所以我的目标是检索一个看起来像这样的字符串

string output =  "Stuff, another thing
                  Random stuff, snuff
                  Pigs are wierd, sick
                  Cats are dangerous, they will kill you
                  Cows produce milk, but horses don't"

试图使用正则表达式来完成此操作,但是如果以一种很好的方式将字符串更长的话,它并不能真正将其正确分组。

string[] output = Regex.Split(a, "^([^,]+(?:,[^,]+){1})");

有什么建议吗?

5 个答案:

答案 0 :(得分:3)

将所有逗号分开,然后重新组合成逗号分隔的对。

using System;

public class Program
{
    public static void Main()
    {
        string  a =  "Stuff, another thing, random stuff, snuff, Pigs are wierd, sick, Cats are dangerous, they will kill you, Cows produce milk, but horses don't";
        string[] splittedStrings = a.Split(new[]{", "}, StringSplitOptions.None);
        for (var i = 0; i < splittedStrings.Length / 2; i++)
        {
            Console.WriteLine(splittedStrings[i*2] + ", " + splittedStrings[i*2 + 1]);
        }

        if(splittedStrings.Length % 2 == 1)
        {
            Console.WriteLine(splittedStrings[splittedStrings.Length-1]);
        }
    }
}

答案 1 :(得分:1)

请尝试使用此代码。您不应该使用正则表达式的分割功能。您应该使用“匹配项”。

        string a = "Stuff, another thing, random stuff, snuff, Pigs are wierd, sick, Cats are dangerous, they will kill you, Cows produce milk, but horses don't";
        var output = Regex.Matches(a, "[^,]+,[^,]+,*");
        StringBuilder sb = new StringBuilder();
        foreach(Match item in output)
        {
            sb.AppendLine(item.Value.Trim(','));
        }
        Console.WriteLine(sb.ToString());

答案 2 :(得分:0)

这是通过c#进行操作的一种方法

  1. 将字符串拆分并在偶数idexe上保留',',因此0 2 4仍将包含逗号

    var splittedWithComasOnEvenIndexes = a.Split(',')。Select((value,index)=> value +(index%2 == 0?“,”:“”));

  2. 连接字符串

    var result = string.Join(“”,splittedWithComasOnEvenIndexes);

结果如下 **

  

东西,另一件事是随机的东西,鼻烟猪很奇怪,生病的猫是   危险,它们会杀死您奶牛会产牛奶,但马不会

**

答案 3 :(得分:0)

我也将拆分和重组,但是我将提供一个更健壮的解决方案,以处理逗号和奇数对。

    public static IEnumerable<string> SplitGroup(this string input, char delimiter)
    {
        var fields = input.Split(delimiter);
        var items = new List<string>();
        var enumerator = fields.GetEnumerator();

        //this will handle no commas, and not and even number of pairs
        while (enumerator.MoveNext())
        {
            var first = enumerator.Current;
            var hasNext = enumerator.MoveNext();
            var second = hasNext ? enumerator.Current : "";

            items.Add(String.Format("{0}{1}{2}", first, hasNext ? "," : "", second));
        }

        return items;
    }

调用扩展方法:

var a = "Stuff, another thing, random stuff, snuff, Pigs are wierd, sick, Cats are dangerous, they will kill you, Cows produce milk, but horses don't, frogs";

var items = a.SplitGroup(',');

答案 4 :(得分:0)

如果放入“ System.Interactive” NuGet程序包,则可以执行以下操作:

var result =
    String.Join(
        Environment.NewLine,
        a.Split(',').Buffer(2).Select(x => String.Join(",", x)));