Unbreakable
Unbreakable

Reputation: 8102

How to format string using input parameter and not placeholders

So, I want to format a string using input parameter and not the index placeholders. Preferably I would like to use a Dictionary. but I am not sure how to implement it.

var str = "I have {Bal} coins";
dictionary.Add("Bal", 20); 
var output =  

Expected Output: I have 20 coins.

I can use String format like below:

var str = "I have {0} coins";
var output = String.Format(str, 20);

My requirement is such that I need to use Dictionary.

Upvotes: 0

Views: 464

Answers (1)

AmirNorsou
AmirNorsou

Reputation: 1131

var str = "I have {0} coins";
var output = String.Format(str, 20);

you can use :

var output = $"I have {variable} coins"; // variable can be dictionary or other

read this post for more : $ in C# string

Upvotes: 1

Related Questions