0

Is there a (simple/predefined) way to use the same string.format mechanism but use names instead of numbers? I want to use the same formatting system but make it more end-user friendly. Right now my users are putting in {0} {1:Minutes} after selecting what parameters they want to use from a list but it becomes quite unreadable and confusing once you start using more then say 2 parameters.

For example want to do {Now} and {Now:Minutes} and have it replaced with DateTime.Now for {Now} and DateTime.Now.Minutes using the IFormattable for {Now:Minutes}. So that the users won't need to select items from a list anymore but rather just type/insert the names of the parameters.

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
Perry
  • 2,250
  • 2
  • 19
  • 24
  • 2
    Just roll your own method using [string.Replace](https://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx) – DavidG Feb 24 '15 at 12:05
  • look at some of the answers here http://stackoverflow.com/questions/21311509/string-format-descriptive-text, especially [this one](http://stackoverflow.com/a/21312156/451540) – thumbmunkeys Feb 24 '15 at 12:05
  • [This](http://humanizr.net/) might be useful to you. – Mord Zuber Feb 24 '15 at 12:24
  • Whilst perhaps not immediately useful to you unless you're using the VS2015 CTP, C# is getting string interpolation which should give you exactly what you need in the future. You can read about it at http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx – Neil Mountford Feb 24 '15 at 12:54
  • possible duplicate of [How to replace tokens on a string template?](http://stackoverflow.com/questions/20651754/how-to-replace-tokens-on-a-string-template) – BrunoLM Mar 08 '15 at 22:54

1 Answers1

0

In you can use

string result = $"Time: {DateTime.Now}";

On previous versions see

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452