Combined source. Use positional args and dictionary together #308
-
I need to format the next string: I've tried to format arguments first, and values from the dictionary next. But it fails trying to resolve {PlayerName} when I provide positional args. What is the best way to format such a case? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
One option is to put positional arguments to the end: var dict = new Dictionary<string, string> { { "PlayerName", "Joe" } };
const string format = "Player {PlayerName} gets {1} coins!";
var formatter = Smart.CreateDefaultSmartFormat();
var result = formatter.Format(format, dict, 1234);
// "Player Joe gets 1234 coins!" |
Beta Was this translation helpful? Give feedback.
-
The question contains the answer already: The second parameter is a list. So, to use elements form this parameter it's necessary to reference the index of the array: [Test]
public void FormatWrapper()
{
var args = new[] { "1", "2", "3" };
const string format = "Player {PlayerName} gets {1[0]} or {1[1]} or {1[2]} coins!";
var dict = new Dictionary<string, string> { { "PlayerName", "Joe" } };
var formatter = Smart.CreateDefaultSmartFormat();
var result = formatter.Format(format, dict, args);
// "Player Joe gets 1 or 2 or 3 coins!"
} (or you could take the |
Beta Was this translation helpful? Give feedback.
One option is to put positional arguments to the end: