Ian G
Ian G

Reputation: 30234

How can I format arguments in a string.format operation

This isn't a serious problem, I was just curious.

I am formatting a string, the data in the output string reuses argument data several times, but changes the case (for example).

string data = "TEST";
string s = string.Format("{0} - {1}", data, data.ToLower());
// REQUIRED OUTPUT 
// TEST - test

But can I achieve this somehow...

// ****PSEUDO-code****
//string s = string.Format("{0} - {0}.ToLower()", data);

Upvotes: 4

Views: 1033

Answers (2)

Oded
Oded

Reputation: 498914

What you want to do is not possible.

When you pass in a string argument to string.Format it is left unchanged (barring alignment/width). There is no way to specify composite formatting that will manipulate a passed in string to change its case.

Upvotes: 2

Andrew Hare
Andrew Hare

Reputation: 351466

There are many specifiers that allow you to format the data that replaces the formatting token (this article is a good place to learn about those).

Unfortunately there is no specifier that lets you do a ToLower on the string - you will have to do that yourself before you pass it to String.Format.

Upvotes: 3

Related Questions