string.Format with strings as params?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everybody,
I need simple string formatting like string.Format() does, but just with
strings what seems not to work.
Can anyone tell me please why this doesn't work and how can it be done? This
looked like the simple and good solution but no.

class Example
{
string format = "{1} {2} {3}"
string anotherFormat = "{3}{1}{2}";

public void Test()
{
Print(format);
Print(anotherFormat);
}

private void Print(string rightFormat)
{
// Here goes FormatException: Input string was not in a correct format
Console.WriteLine(string.Format(rightFormat, "one", "two", "three"));
}
}
Thank you in advance.
 
Hi,

Elements of the format arguments array must be referenced in the format
string just like how an Array is accessed programmatically: with a
zero-based index.

Try "{0} {1} {2}" instead.
 
Hello carry,
string.Format("{0} {1} {2)", "one", "two", "three");

-----------------------------^
Because you have a closing paren in there instead of a closing curly. I
haven't tried to run it, but I can't imagine any other reason.


Oliver Sturm
 
Back
Top