简体   繁体   English

如何在八个字符块中拆分字符串?

[英]How to split a string in chunks of eight chars?

I have following binary string 我有以下二进制字符串

string binaryString = "110011100110011011001110011001101100111001100110110011100110011011001110001001100110011011001110";

Now I want to split the above string into group of 8 characters. 现在我想将上面的字符串拆分为8个字符的组。 eg. 例如。

11001110 11001110 11001110 11001110 11001110 11001110 

I tried following code but not getting expected result 我尝试了下面的代码但没有得到预期的结果

var binary = binaryString.Where((x, y) => y % 8 == 0).ToList();

Try this: 尝试这个:

IEnumerable<string> groups =  Enumerable.Range(0, binaryString.Length / 8)
                                        .Select(i => binaryString.Substring(i * 8, 8));

How about like? 怎么样?

string binaryString = "110011100110011011001110011001101100111001100110110011100110011011001110001001100110011011001110";
int step = binaryString.Length / 8;
for (int i = 0; i < step; i++)
{
    Console.WriteLine(binaryString.Substring(i, 8));
}

Output will be; 输出将是;

11001110
10011100
00111001
01110011
11100110
11001100
10011001
00110011
01100110
11001101
10011011
00110110

Here a DEMO . 这是一个DEMO

List<string> bytes = binaryString.Select((c, i) => new { c, i })
                        .GroupBy(x => x.i / 8)
                        .Select(x => String.Join("",x.Select(y => y.c)))
                        .ToList();

In pure LINQ it's something like this: 在纯LINQ中,它是这样的:

var parts = Enumerable.Range(0, (binaryString.Length + 7) / 8)
                      .Select(p => binaryString.Substring(p * 8, Math.Min(8, binaryString.Length - (p * 8))))
                      .ToArray();

It's quite complex because it's able to "parse" ranges smaller than 8 characters. 它非常复杂,因为它能够“解析”小于8个字符的范围。 So: 所以:

string binaryString = "123456781234567" 

will return 12345678, 1234567 将返回12345678,1234567

(binaryString.Length + 7) / 8)

This is the number of parts we will have. 这是我们将拥有的零件数量。 We have to round up clearly, so "0" is a part, and "123456781" are two parts. 我们必须清楚地整理,所以“0”是一部分,“123456781”是两部分。 By adding (divisor - 1) we are rounding up. 通过添加(除数-1),我们正在四舍五入。

Enumerable.Range(0, (binaryString.Length + 7) / 8)

This will give a sequence of numbers 0, 1, 2, # of parts - 1 这将给出一系列数字0,1,2,#of parts - 1

.Select(p => 

The select :-) 选择:-)

binaryString.Substring(p * 8, Math.Min(8, binaryString.Length - (p * 8))))

The Substring 子串

p * 8

The starting point, the part number * 8, (so part #0 starts at 0, part #1 starts at 8...) 起点,零件号* 8,(因此零件#0从0开始,零件#1从8开始......)

Math.Min(8, binaryString.Length - (p * 8)))

Normally it would be 8, but the last part could be shorter. 通常它会是8,但最后一部分可能会更短。 It means the Min between 8 and the number of remaining characters. 它表示Min8和剩余字符数之间。

With a Regex it's perhaps easier: 使用正则表达式可能更容易:

var parts2 = Regex.Matches(binaryString, ".{1,8}")
                  .OfType<Match>()
                  .Select(p => p.ToString())
                  .ToArray();

(captures groups of 1-8 characters, any character, then take the matches and put them in an array) (捕获1-8个字符的组,任何字符,然后获取匹配并将它们放入数组中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM