String.Format in Visual Basic .NET

vb.net string.formatThe Strings module in Visual Basic .NET contains many procedures to search and manipulate strings of characters. One of these, String.Format(), creates strings by combining string values with other data types and controlling how each value is displayed. Rather than formatting values individually and then concatenating them, Format() works with a string that specifies insertion points in its text. These insertion points are then replaced with the actual values. Being a string itself, the format string can be loaded from external files and resources, allowing for flexible string handling that can be convenient when working with internationalized and localized applications.

To make full use of the information and examples presented here, a working knowledge of programming in Visual Basic.NET is required. Beginners can learn application programming with VB.NET with Microsoft Visual Studio 2013 at Udemy.com. Although the code samples below are written in Visual Basic, String.Format() works just the same in C#. Those who prefer to study C# will find that this C# Tutorial for Beginners is an excellent resource for learning C# programming.

Basic Syntax using Predefined Formats

The usual way to call String.Format() is shown below. Format() is a shared function and you do not need to call it from an instance of the String class.

s = String.Format("Number {0} in Hex: {0:x}", val1)

The first argument to a call to Format() is known as the format string. Any arguments that follow are objects that can be inserted into the string.

The insertion points in curly brackets are replaced with the real values by the function. In the line above, {0} is an insertion point that is replaced with the second object passed into the function (in this example, this object is val1).

The second insertion point in the example above refers to the same object, val1. This time, the number is followed by a colon and then an option that controls how it is displayed in the string. The option x converts the number to hexadecimal format. Using a capital letter, {0:X}, displays the hexadecimal version of the number in capital letters.

Assuming val1 is an integer or the value 255, then the string that is returned by String.Format() in the call above, reads: Number 255 in Hex: ff

The .NET framework defines a fairly large number of formatting options, depending on the data type of the source object. For numbers and Boolean values, you can use:

Format NameDescription
G or gDisplays the number without a thousand separator.
C, or cDisplays the number as a currency value, matching the system’s locale settings.
F or fDisplays the number with at least one digit to the left and right of a decimal separator.
N or nDisplays the number with a thousand separator and with at least one digit to the left of the decimal separator, and at least two digits to the right.
P or pMultiplies the number by 100, and displays it with a % symbol.
E or eDisplays the number in exponential notation.
D or dDisplays the number in decimal (base 10) format.
X or xDisplays the number in hexadecimal (base 16) format.

You can control the number of digits used to display a numeric value by including a number after the formatting option. For example, the following call instructs String.Format() to convert the number to a hexadecimal value using five characters. If the number is too small, it is padded with zeroes until it is the correct size.

s = String.Format("Number {0} in Hex: {0:x5}", val1)

For date and time objects, most of the formatting options display values according to the application’s current locale and culture settings:

Format NameDescription
DDisplays a date and/or time in a long format.
dDisplays a date and/or time in a short format.
fDisplays a date/time value as a long date and a short time.
FDisplays a date/time value as a long date and a long time.
GDisplays a date and/or time.
gDisplays a date/time value as a short date and a short time.
M or mDisplays the month and the day of a date value.
R or rFormats a date according to the RFC1123Pattern property.
sDisplays a date/time value as a sortable index.
TDisplays a time value in a long format.
tDisplays a time value in a short format.
UDisplays a date/time value as a universal sortable index, with a long date and a long time.
uDisplays a date/time value as a universal sortable index.
Y or yFormats a date value as the year and month.

Multiple objects can be passed into Format() by separating them with commas. If you do this, {1} refers to the second object, {2} refers to the third object, and so on.

s = String.Format("Numbers: {0}, {1}, {2}", val1, val2, val3)

Using Custom Number Formats

In addition to the numerous predefined number formats, the options in a format string also enable you to define your own number formatting. This includes padding numbers with leading and trailing digits.

To do this: combine format specifiers after the colon in an insertion point, instead of specifying the format name. The example below displays a double value with at least three digits before the decimal separator, and a maximum of one after the decimal separator.

s = String.Format("{0:000.0}", val1)

If val1 is 25 then the output (on a system that uses a period as a decimal separator) is 025.0. If val1 is 2500.25 then the output is 2500.3.

As a format specifier, ‘0’ is replaced by a digit if one is present in that position in the number. In contrast, the specifier ‘#’ is replaced by a digit if one is present but does not appear in other cases.

When formatting dates and time values, you can build up the format of the string using date/time format specifiers. An example of this is shown below, however, the full list of available specifiers is too large to include here. They can be found on the Microsoft Developer Network page Custom Date and Time Format Strings.

s = String.Format("{0:dd/MM/yyyy}", Now)

String Manipulation

Strings are extremely common when programming in the .NET framework, and using them effectively can significantly impact how well your application performs while running, as well as how easy it is for others to understand you source code. For more examples of Visual Basic .NET’s wide range of features for manipulating and formatting strings, see Learn VB.NET with Microsoft Visual Studio 2013.