This function can be used for various tasks but I happen to use it to make product keys cleaner or easier to read.
public static string sProductKey(string sRawText, int nLength) { string sFinal = ""; for (int i = 0; i <= sRawText.Length; i += nLength) { if (i + nLength < sRawText.Length) { sFinal += sRawText.Substring(i, nLength) + "-"; } else { sFinal += sRawText.Substring(i); } } return sFinal; }
Console.WriteLine(sProductKey("ABCDEFGHIJKLMNOPQRSTUVWXY", 5));
Before: ABCDEFGHIJKLMNOPQRSTUVWXY
After: ABCDE-FGHIJ-KLMNO-PQRST-UVWXY
Last Updated on October 26, 2015