Thursday, May 6, 2010

How to format a string in c# or how to use string.Format?

This String.Format method accepts a format string followed by one to many variables that are to be formatted. The format string consists of placeholders, which are essentially locations to place the value of the variables you pass into the function. These placeholders have the following format:


{indexNumber:formatCharacter}

The below code will convert 1 to 01
string.Format("{0:00}",1)

The Below code will convert a date to the format as "06/05/2010"
string.Format("{0:dd/MM/yyy}",DateTime.Now)

The Below code will convert a date to the formatas "Thursday, May 06, 2010"
string.Format("{0:D}", DateTime.Now)

The Below code will convert a date to the format as “Thursday”
string.Format("{0:dddd}", DateTime.Now)

The Below code will convert the number to currency as “"$1,234.00"”
string.Format("{0:c}", 1234)



You can find more into from the below link.
http://idunno.org/archive/2004/14/01/122.aspx

No comments:

Post a Comment