C# String Formatting Examples

Written by on Sunday 26th September 2010 under C#

C# like many other programming languages uses special format characters when constructing a string. These characters affect they way in which data is presented on the screen.

C and C++ both use sprintf to format a string, you pass in the string with format characters and then the data. The C# equivalent is String.Format.

This method takes as its first parameter a string of text with a list of data type parameters. The format stings however are still a little tricky to remember, hence this little guide on c# string formatting.

Format characters are contained within curly braces and are sequentially numbered.

  1. String.Format("This is a number: {0}", 45);

This will output "This is a number: 45", substituting the first data parameter for the {0}. The second data parameter is substituted for {1}, the third for {2} and so on.

These still are not format strings however, the method simply calls ToString() for each parameter passed in. Format strings could format the 45 into the users currency (for example £45, $45 or ¥45).

In all these examples the zero in {0:} represents the sequential parameter id, and the value of 12345 is passed in.

Numerical Format Strings

CharacterDescriptionUsageExample Output
cCurrency{0:c}£12,345
dDecimal (whole number){0:d}12345
eScientific{0:e}1.234500e+004
fFixed Point{0:f}12345
gGeneral{0:g}12345
nThousand Separator{0:n}12,345
rRound Triple (decimal only){0:r}System.FormatException
xHexadecimal (int only){0:x4}3039

General Formats

You can also use custom format strings, such as decimal placeholders and leading and trailing characters. In these examples we pass in 12345.12.

CharacterDescriptionUsageExample Output
0Zero Placeholder{0:00.0000}12345.1200
#Digit Placeholder{0:(#).##}(12345).12
.Decimal Point{0:0.0}12345.12
,Thousand Separator{0:0,0}12,345
%Percent{0:0%}1234512%
eExponent Placeholder{0:00e+0}12e+3

Date/Time Formats

Date/Time formats are dependant on the users locale, so the output may be different.

CharacterDescriptionUsageExample Output
dShort date{0:d}08/12/2007
DLong date{0:D}08 December 2007
tShort time{0:t}15:27
TLong time{0:T}15:27:40
fLong date time{0:f}08 December 2007 15:27
FLong date time{0:F}08 December 2007 15:27:40
gShort date time{0:g}08/12/2007 15:27
GShort date time{0:G}08/12/2007 15:27:40
MShort date{0:M}08 December
rRFC1123 Date time string{0:r}Sat, 08 Dec 2007 15:27:40 GMT
sSortable date/time{0:s}2007-12-08T15:27:40
uUniversal sortable date{0:u}2007-12-08 15:27:40
UUniversal full date{0:U}08 December 2007 15:27:40
YYear month pattern{0:Y}December 2007

Conditional Formats

You can also set conditions for the numerical values using the semi-colon. The syntax is {0:positive;negative;zero}.

 

Positive, Negative and Zero

  1. // Will output £123
  2. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 123);
  3.  
  4. // Will output £(123)
  5. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", -123);
  6.  
  7. // Will output Zero
  8. string.Format("{0:£#,##0.00;(£#,##0.00);Zero}", 0);

 

Yes/No

  1. // Output Yes
  2. string.Format("{0:Yes;;No}", 1);
  3.  
  4. // Output No
  5. string.Format("{0:Yes;;No}", 0);

More Tutorials in C# Programming

  1. What is C#?
  2. Your First Console Application
  3. Data Types, Variables and Casting
  4. Introducing Methods and the Main() Method
  5. Flow Control and Entry Points
  6. Using the Visual Studio Debugger
  7. Using Method Parameters and Return Values
  8. Access Modifiers and Scope
  9. Introducing Classes and Structs
  10. What are Namespaces?
  11. Interfaces and Classes
  12. Conditional Statements
  13. Looping and Iteration in C#
  14. Using Arrays and Lists in C#
  15. Constants and Read Only Variables
  16. Advanced Data Types
  17. Error and Exception Handling in C#
  18. Using Properties and Indexers
  19. Using Recursion in C#
  20. Event Handling and Delegates
  21. Method Overloading and Overriding
  22. C# Operator List
  23. Class Inheritance
  24. Class Abstraction and Encapsulation
  25. Aggregation and Advanced Scope Techniques
  26. Class and Method Attributes Explained
  27. Class Constructors and Destructors
  28. Polymorphism in C#
  29. Boxing and Unboxing Value Types in C#
  30. Operator Overloading
  31. Creating Multithreaded Applications with C#
  32. Unsafe Code Execution in Microsoft .Net
  33. Generic Variables
  34. XML Serialization and Deserialization
  35. C# String Formatting Examples ⇦ You Are here

Leave a Reply