0

Given an input string such as "KK1234KK" how can I have it print it out with additional formatting: "KK-1234-KK"? In code I had hoped to be able to do something like this:

string LicensePlate = "KK1234KK";

Console.WriteLine(string.Format("Custom: {0:##-####-##}", LicensePlate));
Chris
  • 27,210
  • 6
  • 71
  • 92
Sym_j
  • 7
  • 2
  • Hint: `String.Format` only works with number and date data types. To do this sort of thing, you might want to look into using a Regex. – NightOwl888 Feb 27 '18 at 00:15
  • What is the output you are expecting from that "custom format"? – Rufus L Feb 27 '18 at 00:29
  • I want to insert kk1234kk in Console.ReadLine(), but i want it to be printed in this format: kk-1234-kk. To be added "-" between letters and numbers. – Sym_j Feb 27 '18 at 00:35
  • Reopened since as the comments say `String.Format` will not solve this problem. – Chris Feb 27 '18 at 18:55
  • @Sym_j: I've edited your question to add some text explanation. I hope you are happy with the changes I've made. If you aren't feel free to rollback or further edit it yourself. – Chris Feb 27 '18 at 18:56

1 Answers1

1

You can try using regex expression to get it in this format. Here I'm looking for the number and adding "-" on both sides.

        string LicensePlate = "KK1234KK";
        Regex regex = new Regex(@"\d+");
        Match match = regex.Match(LicensePlate);
        string output = Regex.Replace(LicensePlate, @"\d+","-"+match.Value+"-");
Sudeep Reddy
  • 611
  • 7
  • 8