Click here to Skip to main content
15,890,557 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Finally Understand String.Format()

Rate me:
Please Sign up or sign in to vote.
3.90/5 (7 votes)
8 Oct 2015CPOL2 min read 8.8K   5  
Explain the instruction-sections of the placeholders within a formatstring more clearly than MSDN-Documentation does

Introduction

I think I'm not the only one who has always had trouble when it comes to using String.Format(). I never had a clear understanding about the meaning of its interpunction: """, "", ",", ":", "#", "-", ";" - but finally, I got it. :-)

The Topic: String.Format()

String.Format() accepts a formatstring and a list (of arbitrary length) with arguments, to which the format is to apply. Within the formatstring, there can be arbitrary text, and there are embedded placeholders, marked up with {}, where the formatted arguments get inserted. For example:

VB.NET
Dim s = String.Format("Now it's {0,-20:hh:mm} - hurry up!", Date.Now)

This tips topic is the formatting-instructions within those Placeholders - only in general.
For detailed information, especially the particular type-depending instruction-syntax, refer to the MSDN-documentation - see links below.

The Three Instruction-sections Within a formatstring-placeholder

Within a formatstring-Placeholder, there are 3 sections. The first section is required, the others are optional. The second section is separated by ",", and the third is separated by ":".

Meanings of the Three Sections

  1. The zero-based Index of the aimed argument in the further argument-list: required
  2. "," (if present) separates a section to define width and alignment of the inserted formatted value-string.
    In the sample above a width of 20 chars is defined, and the "-" defines left alignment (Default-alignment is right).
  3. ":" (if present) separates a section to define a particular type-depending Formatting-syntax, which is well-documented on MSDN.
    In the sample above DateTime-depending syntax defines an output of hours and minutes, separated by ":"

The confusion is that the latter two sections are optional, and moreover the inner syntax of the third section changes from type to type, and moreover it may as well use "," or ":", so that especially these two ControlChars can have different meanings within the same formatstring.
But once understood that their first occurrence within the placeholder is as Separator, the confusion hopefully vanishes. :-)

Some Links to MSDN-Documentation

Points of Interest

  • Several other methods also support formatstrings. E.g., Console.WriteLine(string, <argList>) and every TextWriter-Derivate (StreamWriter, XmlWriter,...)
  • The type-depending syntax is as well valid the .Tostring(formatstring) - Overloads of the particular DataType in speech.
    That affects only the 3. section - the column-definition-syntax is not valid.
    A sample:
    VB.NET
    Dim s = Date.Now.ToString("hh:mm")

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --