Repository navigation
Combined source. Use positional args and dictionary together #308
Answered
by
axunonb
ViacheslavRud
asked this question in
Q&A
|
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? |
Answered by
axunonb
Aug 20, 2022
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!" |
4 replies
Answer selected by
axunonb
|
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 |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One option is to put positional arguments to the end: