Format a MAC address using string.Format in c#
c#, string
Solution
Reformat a string to display it as a MAC address:
var macadres = "0018103AB839";
var regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
var replace = "$1:$2:$3:$4:$5:$6";
var newformat = Regex.Replace(macadres, regex, replace);
// newformat = "00:18:10:3A:B8:39"
If you want to validate the input string use this regex (thanks to J0HN):
var regex = String.Concat(Enumerable.Repeat("([a-fA-F0-9]{2})", 6));
Problem
I have a mac address which is formatted like `0018103AB839` and I want to display it like: `00:18:10:3A:B8:39` I am trying to do this with `string.Format` but I cant really find the exact syntax. right now I am trying something like this: ``` string macaddress = 0018103AB839; string newformat = string.Format("{0:00:00:00:00:00:00}", macaddress); ``` Is this even possible? or should I use `string.Insert`?