0

Using .Net Core 2.0 / C# can you interpolate a string retrieved from a file? Here's an example of what I would like to do. My file contains {simpleString} which should be replaced with "Test Value".

    string template = string.Empty;
    string simpleString = "Test Value";
    string interpolatedString = $"Name: {simpleString}";

    //interpolatedString is now "Name: Test Value"

    string filePath = Path.GetFullPath("Templates/ToEnrollee.html");
    using (StreamReader reader = new StreamReader(filePath))
    {
        template = reader.ReadToEnd();
    }
    //I would like interpolation to take place here
    string body = string.Format(template);
Troy Turley
  • 659
  • 10
  • 28
  • 1
    Use old style format, store the string as `"Name: {0}"` and use `string.Format(template, simpleString);` – Gusman Jan 24 '18 at 18:52
  • why using string.Format when you want to use interpolation?you can use the old string.Format syntax to replace the value – Niladri Jan 24 '18 at 18:53
  • 2
    No - the interpolation happens at compile-time. (see marked duplicate) – D Stanley Jan 24 '18 at 18:56
  • 1
    you will find several people have posted string.format variations that use names rather than numbers, taking the values from a dictionary. here for example https://stackoverflow.com/questions/36759694/is-there-a-string-format-that-can-accept-named-input-parameters-instead-of-ind – pm100 Jan 24 '18 at 19:04

0 Answers0