String value
String value = "11100110100";
I want to split it like shown below,
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?
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.