Add separator to string at every N characters?

后端 未结 13 2290
半阙折子戏
半阙折子戏 2020-11-27 13:20

I have a string which contains binary digits. How to separate string after each 8 digit?

Suppose the string is:

string x = \"111111110000000011111111         


        
13条回答
  •  没有蜡笔的小新
    2020-11-27 13:32

    One way using LINQ:

    string data = "111111110000000011111111000000001111111100000000";
    const int separateOnLength = 8;
    
    string separated = new string(
        data.Select((x,i) => i > 0 && i % separateOnLength == 0 ? new [] { ',', x } : new [] { x })
            .SelectMany(x => x)
            .ToArray()
        );
    

提交回复
热议问题