[英]c# split string value by numeric
String value 字符串值
String value = "11100110100";
I want to split it like shown below, 我想拆分它,如下图所示,
111,00,11,0,1,00 111,00,11,0,1,00
I tried that by splitting based on the numbers, as shown below: 我尝试通过基于数字分割,如下所示:
List<string> result1= value.Split('0').ToList<string>();
List<string> result2= value.Split('1').ToList<string>();
It did not work so, how can i get the desired output (shown below) by splitting 1 and 0? 它不起作用,如何通过拆分1和0获得所需的输出(如下所示)?
111
00
11
0
1
00
Thanks. 谢谢。
您可以在每个更改之间放置一个字符,从0到1,从1到0,然后拆分:
string[] result = value.Replace("10", "1,0").Replace("01", "0,1").Split(',');
Here is my extension method, without replacing - only parsing. 这是我的扩展方法,没有替换 - 只解析。
public static IEnumerable<string> Group(this string s)
{
if (s == null) throw new ArgumentNullException("s");
var index = 0;
while (index < s.Length)
{
var currentGroupChar = s[index];
int groupSize = 1;
while (index + 1 < s.Length && currentGroupChar == s[index + 1])
{
groupSize += 1;
index += 1;
}
index += 1;
yield return new string(currentGroupChar, groupSize);
}
}
Note: it works for every char grouping (not only 0 and 1) 注意:它适用于每个char分组 (不仅仅是0和1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.