-1

In C# I've a case where I'm looking to generalize string formatting of the following type:

string original = "[{0}] is a parent of [{1}] who is the brother of [{2}] who is..";
List<string> args = new List<string>() {"Alice", "Bob", "Charlie", "Doug"};

string formattedString = String.Format(original, args);

Expected output would be:

Alice is a parent of Bob who is the brother of Charlie who is..

The main challenge here is, original string can have unknown number of variables, given that List args is always supposed to have higher or equal count than the number of variables in original string, how can I make this work?

16kp
  • 99
  • 1
  • 2
  • 9
  • It isn't clear what you are trying to achieve. How do you determine if the original string is a good candidate for the input args? And if isn't, what do you expect to happen? You've only described the case for an exact match. – David L Dec 31 '20 at 05:47
  • `String.Format` already supports this if you convert the list to a `string[]` and remove the `[]` in the string. – Sweeper Dec 31 '20 at 05:54
  • Ignoring the already mentioned code errors, you need to clarify what you mean by … _” how can I make this work?”_ … where the _”`original` string can have an unknown number of variables”_… ? What is your current code doing now? It has three (3) variables in the original string. The same code would work with 1, 5 or an “unknown” number of variables. You should clarify “what” you are asking. If the `original` string could have an unknown number of variables… then “how” is this string “constructed?” And why would you care “how” many variables are in it? The code would still work. – JohnG Dec 31 '20 at 06:18
  • @00110001 , DavidL, I think it is pretty clear that the question is about dealing with a format string, containing an uncertain number of indices, and the requirement to provide a list of arguments, containing at least as many elements, as the maximum index in a format string. Of course, if KrishnaPurohit meant something else, then you are correct and the question is not clear at all :D – user4182984 Dec 31 '20 at 06:39
  • @JKlen … I will assume you missed the last sentence in the question. There is an assumption the `args` list is of a valid size. This makes the question unclear since the current code (if fixed) would still work with any number of arguments that is within the commands range. – JohnG Dec 31 '20 at 06:47
  • @JohnG Oh, I interpreted this as "given that .NET requires the list to be of valid size". Damn. You guys are correct—the question does need more clarity. – user4182984 Dec 31 '20 at 06:50

2 Answers2

3
string original = "{0} is a parent of {1} who is the brother of {2} who is..";
string[] args = new string[] { "Alice", "Bob", "Charlie", "Doug" };

string formattedString = String.Format(original, args);

Try the above code it will provide you your expected output.
I just changed List to string[].

Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
0

Perhaps the best (most robust) way of counting the number of indices in a format string would be to mimic the way it is done internally in AppendFormatHelper. I won't list the code here—it is well over 200 lines long—but the gist of it is: iterate over characters and check whether each is a curly brace, but not an escaped curly brace and so on.

An easier (albeit more prone to error) way would be to use regex to count them, in which case I am going to direct you towards this question.

user4182984
  • 222
  • 2
  • 9