1. Technology

Formatting Strings, Numbers, and Other Objects

Formatting Strings with the ToString method


Before discussing some of the other Format options, keep in mind that there's still yet another way to format an object that might be easier for you: the ToString method. And it doesn't even use the Format keyword.

Recall that ToString is one of the fundamental methods in the grandaddy of all objects: the Object object. This means that it's inherited by all objects. Overloaded ToString methods for objects that inherit ToString let you format the string that results from the ToString method.

Let's start with the same example we have already looked at. Here's how to use ToString to format a date:

Dim theDate As Date = #12/7/1941#
TextBox1.Text = theDate.ToString("MMMM d, yyyy")

Adding Culture Information

More good news: adding culture information is easy! Suppose you want to display the date from a structure in, say, Spain. Just add a CultureInfo object.

Dim MyCulture As _
     New System.Globalization.CultureInfo("es-ES")
CultureDateEcho.Text = _
     theDate.ToString("MMMM d, yyyy", MyCulture)

The result is:

diciembre 7, 1941

The culture code is a property of the MyCulture object. The CultureInfo object is an example of a provider. The constant "es-ES" isn't being passed as a parameter; an instance of the CultureInfo object is. Search the VB.NET Help system for CultureInfo to see the list of supported cultures.

You can also use a CultureInfo object with the Format method. The code to do this is as follows:

Dim MyCulture As _
     New System.Globalization.CultureInfo("es-ES")
CultureFormatDateEcho.Text = _
          MyCulture, "{0:MMMM d, yyyy}", theDate)

All five ways to format a date are shown in the illustration.

Click Here to display the illustration

Format Specifiers and the Format Method

A lot of the power and flexibility of the Format method is a result of the use of format specifiers. Just to be clear, here's that definition again:


The format specifier we've been using so far is:

"{0:MMMM d, yyyy}"

One of the five overloads of the Format method that we looked at earlier is:

String.Format(String, Object, Object, Object)

The "format specifier" above is the String argument in the syntax definition above.

All of the permissable format specifiers are defined in .NET but there are so many that you will probably have to look at a reference to remember them. Look up "Formatting Types" at MSDN to see the wide array of choices.

Here's a few examples of formatting numbers to give you a flavor of what can be done.

Big numbers are usually displayed using an "exponential" format. Suppose you wanted to display the national debt of the U.S. (as of September 11, 2009) in dollars. You could write:


or you could write:


The second example might be more appropriate these days since it's such a huge number. But you can use a format specifier to display either one:

Dim NatDebt As Int64 = 11799461425415
MsgBox(String.Format("The debt is {0:C} in numbers or " &
    "{0:E2} in scientific notation.", NatDebt))

(It's such a big number that a 32 bit integer isn't big enough.)

Notice that the index 0 is used twice in the format specifier and the same value in NatDebt is plugged into both.

The "index" in the format specifier has been equal to 0 in the examples so far. This way of identifying substitution variables goes back a long way but might be unfamiliar to some people learning VB.NET. Here's a more complex example that shows how to use both the index and StringBuilder to build a report from multiple elements. If you were reading values from a database, you could build a complex output the same way.

Dim DailyNatDebt As Int64
Dim i As Int16
Dim DebtDisplay As New System.Text.StringBuilder
DebtDisplay.Append(String.Format("From {0:D} " & vbCrLf &
     " to {1:D} " & vbCrLf &
     " the U. S. national debt was: " & vbCrLf,
     Today.AddDays(1), Today.AddDays(9)))
DailyNatDebt = 11799461425415
For i = 1 To 9
     DailyNatDebt += 3910000000
          String.Format("{0:C} on {1:D}" &
          vbCrLf, DailyNatDebt, Today.AddDays(i)))

DailyNatDebt is plugged into the 0 index and Today is plugged into the 1 index. (Yes, I know. The debt numbers are different now. Also, as the article StringBuilder ... A New Object in .NET shows, using StringBuilder throughout to avoid the use of string concatenation using the & operator would have been more efficient, but the constant vbCrLf can't be embedded in the format specifier!)

Click Here to display the illustration

VB.NET Format doesn't work for everything, however. On the next page, we consider a few instances where it doesn't work.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Formatting Strings, Numbers, and Other Objects

©2014 About.com. All rights reserved.