3

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); } 
3
  • Does this answer your question? Using variables inside strings Commented Nov 22, 2022 at 15:34
  • And stackoverflow.com/questions/36759694, The chain of dupe is too long on that one Commented Nov 22, 2022 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). Commented Nov 22, 2022 at 17:42

4 Answers 4

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"))); 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it helped me. I think the Replace method is the best way to solve my problem. Thank you
In the end I use Replace("{" + nameof(name) + "}", name)
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

Comments

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

1 Comment

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.
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!

3 Comments

doesn't this makes the entire GetHelloGreeting method obsolete ?=!
@MongZhu I wanted to answer the way the question was asked, otherwise it will be answered: Console.WriteLine("Hello, {0}!",name)
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.