2

I'm beginer in C#. Now I have next task: In method I get template and arguments and I have to return formated string.

For example:

template = "Hello, {name}!"

name = "Bob"

So result must be a string -> Hello, Bob!

public static string GetHelloGreeting(string template, string name)
{
    return string.Format(template, name);
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Does this answer your question? [Using variables inside strings](https://stackoverflow.com/questions/7227413/using-variables-inside-strings) – Drag and Drop Nov 22 '22 at 15:34
  • And https://stackoverflow.com/questions/36759694/, The chain of dupe is too long on that one – Drag and Drop Nov 22 '22 at 15:36
  • @DragandDrop: string interplation is not what OP is asking for. You can just use it directly, not if you get a format string like in `GetHelloGreeting`. `String.Format` can do that, but only with indexes not with names. While some answers(in your links) suggest third party libraries which support it, it will not help OP because he said that he's a beginner and got a task(probably from the teacher). – Tim Schmelter Nov 22 '22 at 17:42

4 Answers4

3

String.Format expects an index in the braces. You want to pass the name in it, so you can replace it with the actual name value. I'd suggest to use String.Replace:

public static string GetHelloGreeting(string template, string name)
{
    return template.Replace("{name}", name);
}

You could provide a method which is more reusable. For example:

public static string ReplaceAll(string template, params (string key, string value)[] replacements)
{
    foreach (var kv in replacements)
    {
        template = template.Replace("{"+ kv.key + "}", kv.value);
    }

    return template;
}

Your example:

string res = ReplaceAll("Hello, {name}!", ("name", "Bob"));

but also possible with multiple parameters:

string res = ReplaceAll("Hello, {name}! Now it's {time}", ("name", "Bob"), ("time", DateTime.Now.ToString("HH:mm")));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

when specifying a format you use an index for the parameters that will follow. It is called a composite format string:

string template = "Hello, {0}!"

this makes it independent of variable names. But the true reason is that the overload of the Format method that you are using takes a params array as parameter as you can see in the method signature:

public static string Format (string format, params object?[] args);

so the index that is found in the template will be applied to extract the objects on the appropriate places from the array of objects that you pass into the method

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I have different test methods that my method must conform to. In them, the `template` looks exactly as I have shown. I thought replace name of parameter to index but in next method I have the same task but with 2 params with different position. – Volodymyr Hula Nov 23 '22 at 08:48
0

The value of your template parameter will have to change somehow. If you want to use string interpolation, this answer shows that. So template = $"Hello, {name}"; in which case you wouldn't need to use String.Format at all. Just make sure you define name before you define template.

Or you could use String.Format(template, name); like you have but you would need template = "Hello, {0}!"; The 0 is the index of the variable that will go in that position. For more info see String.Format

frankM_DN
  • 365
  • 7
0

If you want to use string.Format(), the template must be correct. Add the character $ at the beginning of the template:

try this:

  string name = "Bob";
  string template = $"Hello, {name}!";

  Console.WriteLine(GetHelloGreeting(template, name)); // Hello, Bob!
     
  public static string GetHelloGreeting(string template, string name)
  {
       return string.Format(template, name);
  }

reult:

Hello, Bob!

Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • doesn't this makes the entire `GetHelloGreeting` method obsolete ?=! – Mong Zhu Nov 22 '22 at 15:28
  • @MongZhu I wanted to answer the way the question was asked, otherwise it will be answered: `Console.WriteLine("Hello, {0}!",name)` – Hossein Sabziani Nov 22 '22 at 15:39
  • what I wanted to say is that the complete string is already in `template` before you pass it into the method. the string.format call does not do anything extra – Mong Zhu Nov 22 '22 at 16:03