Facebook Twitter

Compiled » String Formatting in C# I couldn’t find a quick reference to .NET string formatting using the String.Format() function, so I created this one (which has also spawned this String Formatting FAQ).

Compiled » String Formatting in C#

When I started working with the .NET framework, one thing puzzled me. I couldn’t find sprintf(). sprintf() is the C function that takes an output buffer, a format string, and any number of arguments, and builds a string for you. For example: char szError[256];sprintf(szError, “Error %d occurred. \n”, nError); This would write “Error 12 occurred.” into the szError buffer (assuming nError was 12). Str << “Error ” << nError << ” occurred.” << endl; Or something close to that. The .NET framework handles strings very nicely – but it takes some getting used to. String errorString = String.Format(“Error {0} occurred.”, nError); Teeming with metadata, the .NET environment doesn’t need the format string to say what type of data you’re formatting, just where you want it.

Strings Numbers Basic number formatting specifiers: Dates. Main Page. Main Page - MonoDevelop.