0

Lets say I have the following string:

1111000011001101

How can I extract substrings of 4 chars until the end of the string so that I have the following array ?

|1111|0000|1100|1101|
En_Tech_Siast
  • 83
  • 1
  • 12

2 Answers2

1

Because Strings are only char[]'s you can use the String.Substring(int a, int b) method to retrieve the number of characters specified (int b) from a character position in the string (int a).

If you wanted the first four characters of your string you could use

String s = "1111000011001101";
String firstFourChars = s.Substring(0,4);
JEllery
  • 304
  • 2
  • 13
0

Playing around with LinqPad I get the following:

var s = "1111000011001101";

var substrings = Enumerable
  .Range(0, s.Length / 4)
  .Select(i => s.Substring(i * 4, 4))
  .ToArray();

substrings.Dump();

Output:

String[] (4 items)
1111 
0000 

1100 
1101 

EDIT: Just noticed this is a duplicate of answer -- see link in the comments of OP. See that link as it gives a general solution.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Thane Plummer
  • 7,966
  • 3
  • 26
  • 30