Click here to Skip to main content
13,142,622 members (53,780 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 3 Aug 2011

String Formatting Made Easy for .NET

, 3 Aug 2011
Rate this:
Please Sign up or sign in to vote.
String formatting made easy for .NET

Everyone who is used to .NET Framework knows string.format. It's quite powerful in formatting all kinds of objects and also has some aligning features. I wrote my own simple string formatting class, with slightly different alignment possibilities, with text macro support and missing macro callbacks.

The reason for writing this formatter was to easily format text for text-based customer displays, which in fact is not a very challenging task, and to format text for text-based receipt printers.

Formatting Syntax

The syntax is similar to the syntax used by string.Format. Each format-packet is enclosed by curly brackets and contains colon separated fields. The fields are as follows:

  1. Macro-name, text or index: Macro-names are enclosed by square brackets and text is enclosed by double quotes
  2. Padding specifier:
    • L - Left-aligned not filled, no padding. Default if no padding is specified. Text does not get truncated.
    • LF - Left-aligned and fill. Text gets left aligned and missing characters are filled up with the padding character. The text gets truncated if it is longer than the specified padding length
    • M - Centered output. The text gets centered by the specified padding length and gets truncated id the text is longer than the padding length.
    • R - Right-aligned output. The text gets right aligned by the specified padding length and is truncated if the text is longer than the specified padding length.
  3. Padding Width: Specifies the padding width as used by the corresponding padding specifier
  4. Padding character: Specifies the padding character used for padding. Use only a single character, no quoting.


The following code:

f.Format("{0:LF:10:-}{1:R:10:*}", "Hello", "World")



This shows the left-aligned and right-aligned text output. More padding samples can be found in the source code.

The above code samples use the indices (as also used by string.format) to reference the string to format, but the simple formatter also supports text macros. Once a macro is registered, it can be used as shown and produces the expected output:

f.DefineTextMacro("hello", "HELLO");

But also if a used macro is not registered, you can register to receive an event once an unregistered macro is used as shown below:

f.OnGetParameter += OnGetParameter;
private static string OnGetParameter (string parameterName){
  //Return some parameter values

For each instance of [universe], the OnGetParameter event is raised and the code may return different values at different times.

Source Code

Check out the code at git clone git://

Run the samples to see how it works.
The formatter is located at Deveck.Utils/StringUtils/StringFormatter.cs.


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Austria Austria
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 3 Aug 2011
Article Copyright 2011 by deveck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid