1

I'm having the string value 740D1E11625C89019E61D0CC I want to insert "-" in string after every count 5 length.

I'm using

ReplaceAt(serial, 5, 0, "-");


public static string ReplaceAt(string str, int index, int length, string replace)
        {
            return str.Remove(index, Math.Min(length, str.Length - index))
                    .Insert(index, replace);
        }

Result

740D1-E11625C89019E61D0CC

It should be

740D1-E1162-5C890-19E61-D0CC

Is this right way to modify string ?

Sunil Acharya
  • 1,153
  • 5
  • 22
  • 39

1 Answers1

1

You can use Batch from MoreLINQ. It has a Nuget package as well.

var s = "740D1E11625C89019E61D0CC";
var array = s.Batch(5, seq => new string(seq.ToArray()));
Console.WriteLine(string.Join("-", array));

prints

740D1-E1162-5C890-19E61-D0CC
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364