string.Format with strings as params?

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.
 
D

Dave Sexton

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.
 
O

Oliver Sturm

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top