By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

string.Format with strings as params?

P: n/a
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.
Dec 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ok, as i tried to ilustrate the problem, I found my example working (+ wrong
inexes - should be from {0} of course), but this piece doesn't work for sure:

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

.... and i still wonder why :-/
Dec 9 '06 #2

P: n/a
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.

--
Dave Sexton

"carry" <ca***@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
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.

Dec 9 '06 #3

P: n/a
Count the pairs of { and }
"carry" <ca***@discussions.microsoft.comwrote in message
news:FB**********************************@microsof t.com...
Ok, as i tried to ilustrate the problem, I found my example working (+
wrong
inexes - should be from {0} of course), but this piece doesn't work for
sure:

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

... and i still wonder why :-/

Dec 10 '06 #4

P: n/a
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
--
http://www.sturmnet.org/blog
Dec 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.