C# C在长字符串上每隔一个逗号拆分字符串
我有一个字符串,看起来像这样:C# C在长字符串上每隔一个逗号拆分字符串,c#,.net,string,split,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 Ra
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})");
有什么建议吗 将其沿所有逗号拆分,然后重新组合成逗号分隔的对
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]);
}
}
}
将其沿所有逗号拆分,然后重新组合成逗号分隔的对
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]);
}
}
}
这里有一种通过c实现的方法 拆分字符串并将“,”保留在偶数idex上,以便0 2 4仍包含逗号 var SplittedWithComAsonevenIndex=a.Split','。选择值,索引=>value+索引%2==0?,: 把绳子连接起来 var result=string.Join,用comasonevenindex拆分 结果如下 ** 东西,另一件随机的东西,鼻烟猪很瘦,病猫很瘦 危险的是,他们会杀了你奶牛会产奶,但马不会
**这里有一种通过c实现的方法 拆分字符串并将“,”保留在偶数idex上,以便0 2 4仍包含逗号 var SplittedWithComAsonevenIndex=a.Split','。选择值,索引=>value+索引%2==0?,: 把绳子连接起来 var result=string.Join,用comasonevenindex拆分 结果如下 ** 东西,另一件随机的东西,鼻烟猪很瘦,病猫很瘦 危险的是,他们会杀了你奶牛会产奶,但马不会
**请尝试此代码。您不应该使用正则表达式的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";
var output = Regex.Matches(a, "[^,]+,[^,]+,*");
StringBuilder sb = new StringBuilder();
foreach(Match item in output)
{
sb.AppendLine(item.Value.Trim(','));
}
Console.WriteLine(sb.ToString());
请尝试此代码。您不应该使用正则表达式的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";
var output = Regex.Matches(a, "[^,]+,[^,]+,*");
StringBuilder sb = new StringBuilder();
foreach(Match item in output)
{
sb.AppendLine(item.Value.Trim(','));
}
Console.WriteLine(sb.ToString());
我也会拆分和重新组装,但我会制作一个更健壮的解决方案来处理无逗号和奇数对
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;
}
我也会拆分和重新组装,但我会制作一个更健壮的解决方案来处理无逗号和奇数对
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;
}
如果插入System.Interactive NuGet软件包,则可以执行以下操作:
var result =
String.Join(
Environment.NewLine,
a.Split(',').Buffer(2).Select(x => String.Join(",", x)));
如果插入System.Interactive NuGet软件包,则可以执行以下操作:
var result =
String.Join(
Environment.NewLine,
a.Split(',').Buffer(2).Select(x => String.Join(",", x)));
问题提到,他们需要每隔一个逗号,因此偶数indexomma分隔对的所有逗号=item1,item2。作为旁注,问题没有指定如果最后一个索引为偶数,是否需要逗号。问题提到,他们需要每隔一个逗号,因此偶数索引的所有逗号逗号分隔对=item1,item2。作为旁注,该问题没有指定如果最后一个索引为偶数,是否需要逗号。