Now how about this for a general solution?
// uglified code to fit within horizontal limits public static string InsertAtIndices (this string original, string insertion, params int[] insertionPoints) { var mutable = new StringBuilder(original); var validInsertionPoints = insertionPoints .Distinct() .Where(i => i >= 0 && i < original.Length) .OrderByDescending(i => i); foreach (int insertionPoint in validInsertionPoints) mutable.Insert(insertionPoint, insertion); return mutable.ToString(); }
Using:
string ssn = "832984566".InsertAtIndices("-", 3, 5); string crazy = "42387542342309856340924803" .InsertAtIndices(":", 1, 2, 3, 4, 5, 6, 17, 200, -1, -1, 2, 3, 3, 4); Console.WriteLine(ssn); Console.WriteLine(crazy);
Conclusion:
832-98-4566
4: 2: 3: 8: 7: 5: 42342309856: 340924803
Overkill? Yes, may be...
<sub> PS Yes, I am a regular hero illiterate - something I hope to fix someday.
source share