Add a separator to a string for every N characters?

I have a string that contains binary digits. How to separate a line after each 8 digits?

Assume the line:

string x = "111111110000000011111111000000001111111100000000"; 

I want to add a separator like, (comma) after each 8 characters.

the conclusion should be:

 "11111111,00000000,11111111,00000000,11111111,00000000," 

Then I want to send it to the list <> last 8 char 1st, then the previous 8 characters (except,), etc.

How can i do this?

+49
string c # grouping string-formatting
Mar 29 2018-12-12T00:
source share
10 answers
 Regex.Replace(myString, ".{8}", "$0,"); 

If you need an array of eight characters, then probably easier:

 Regex.Split(myString, "(?<=^(.{8})+)"); 

which will split the line only at those points where there will be a multiple of eight characters in front of it.

+90
Mar 29 '12 at 19:29
source share

Try the following:

 var s = "111111110000000011111111000000001111111100000000"; var list = Enumerable .Range(0, s.Length/8) .Select(i => s.Substring(i*8, 8)) .ToList(); var res = string.Join(",", list); 
+27
Mar 29 '12 at 19:30
source share

If I understand your last requirement correctly (it’s not clear to me whether an intermediate line with comma delimited or not is needed), you can do this:

 var enumerable = "111111110000000011111111000000001111111100000000".Batch(8).Reverse(); 

Using morelinq .

+2
Mar 29 '12 at 19:36
source share

There is another Regex approach:

 var str = "111111110000000011111111000000001111111100000000"; # for .NET 4 var res = String.Join(",",Regex.Matches(str, @"\d{8}").Cast<Match>()); # for .NET 3.5 var res = String.Join(",", Regex.Matches(str, @"\d{8}") .OfType<Match>() .Select(m => m.Value).ToArray()); 
+2
Mar 29 2018-12-19T00:
source share

Ugly but less junk:

 private string InsertStrings(string s, int insertEvery, char insert) { char[] ins = s.ToCharArray(); int length = s.Length + (s.Length / insertEvery); if (ins.Length % insertEvery == 0) { length--; } var outs = new char[length]; long di = 0; long si = 0; while (si < s.Length - insertEvery) { Array.Copy(ins, si, outs, di, insertEvery); si += insertEvery; di += insertEvery; outs[di] = insert; di ++; } Array.Copy(ins, si, outs, di, ins.Length - si); return new string(outs); } 

Line overload:

 private string InsertStrings(string s, int insertEvery, string insert) { char[] ins = s.ToCharArray(); char[] inserts = insert.ToCharArray(); int insertLength = inserts.Length; int length = s.Length + (s.Length / insertEvery) * insert.Length; if (ins.Length % insertEvery == 0) { length -= insert.Length; } var outs = new char[length]; long di = 0; long si = 0; while (si < s.Length - insertEvery) { Array.Copy(ins, si, outs, di, insertEvery); si += insertEvery; di += insertEvery; Array.Copy(inserts, 0, outs, di, insertLength); di += insertLength; } Array.Copy(ins, si, outs, di, ins.Length - si); return new string(outs); } 
+2
Apr 21 '14 at 20:27
source share

One way to use 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() ); 
+1
Mar 29 '12 at 19:37
source share

... or old school:

 public static List<string> splitter(string in, out string csv) { if (in.length % 8 != 0) throw new ArgumentException("in"); var lst = new List<string>(in/8); for (int i=0; i < in.length / 8; i++) lst.Add(in.Substring(i*8,8)); csv = string.Join(",", lst); //This we want in input order (I believe) lst.Reverse(); //As we want list in reverse order (I believe) return lst; } 
+1
Mar 29 '12 at 19:43
source share

It is much faster without copying the array (this version inserts a space every 3 digits, but you can customize it according to your needs)

 public string GetString(double valueField) { char[] ins = valueField.ToString().ToCharArray(); int length = ins.Length + (ins.Length / 3); if (ins.Length % 3 == 0) { length--; } char[] outs = new char[length]; int i = length - 1; int j = ins.Length - 1; int k = 0; do { if (k == 3) { outs[i--] = ' '; k = 0; } else { outs[i--] = ins[j--]; k++; } } while (i >= 0); return new string(outs); } 
0
Aug 26 '14 at 15:07
source share

A bit late for the party, but here's a simplified LINQ expression to split the input string x into groups of n , separated by another sep string:

 string sep = ","; int n = 8; string result = String.Join(sep, x.InSetsOf(n).Select(g => new String(g.ToArray()))); 

A quick summary of what is happening here:

  • x considered as an IEnumberable<char> , where the InSetsOf extension method is located.
  • InSetsOf(n) groups characters in IEnumerable of IEnumerable - each entry in the outer grouping contains an inner character group n .
  • Inside the Select method, each group of n characters is returned to a string using the String() constructor, which takes a chars array.
  • The result of Select is now an IEnumerable<string> , which is passed to String.Join to alternate the string sep , like any other example.
0
Aug 13 '15 at 15:30
source share

I'm more than late with my answer, but you can use this:

  static string PutLineBreak(string str, int split) { for (int a = 1; a <= str.Length; a++) { if (a % split == 0) str = str.Insert(a, "\n"); } return str; } 
0
Dec 09 '17 at 11:50
source share



All Articles