VB.NET String.Format

Format

Format strings can compose larger Strings from different values. With the String.Format Function, we can use format strings to create useful string representations.
We specify numeric formats,
date formats
and even padding.

Example

String face

We pass a format String as the first argument to String.Format. The String.Format function returns the fully composed String. In the format pattern, we see the marker {0}, which means the second argument is to be inserted there.

DateTime

And:The marker {1:0.0} means the third argument is to be inserted there, with one digit after the decimal place.

Finally:The marker {2:yyyy} means the fourth argument is to be inserted there, with only the year digits being added.

Based on:

.NET 4.0

Program that uses format String: VB.NET

Module Module1
    Sub Main()
	Dim name As String = "Dot Net Perls"
	Dim number As Integer = 10000
	Dim day As DateTime = New DateTime(2007, 11, 1)

	' Format with String.Format.
	Dim result As String = String.Format("{0}: {1:0.0} - {2:yyyy}", _
					     name, number, day)

	' Write result.
	Console.WriteLine(result)
    End Sub
End Module

Output

Dot Net Perls: 10000.0 - 2007

Percentages

Percentage symbol

Continuing on, we can see that percentages can also be used with format strings. With the format pattern {0:0.0%}, the second argument is inserted as a percentage. It is multiplied by 100 to yield 73.0% from the number 0.73.

Caution:If you pass a 100-based percentage figure to String.Format, you will get a percentage that is larger than you might want.

Program that uses percentage String: VB.NET

Module Module1
    Sub Main()
	' Convert Double to percentage String.
	Dim ratio As Double = 0.73
	Dim result As String = String.Format("String = {0:0.0%}", _
					     ratio)
	Console.WriteLine(result)
    End Sub
End Module

Output

String = 73.0%

Padding

Padding: right and left

Padding, which is whitespace that is inserted to fill a certain number of characters, is also available through format strings. With the substitution marker {0,-10}, the string is padded to ten characters with the spaces on the right.

And:With the marker {1,10}, the string is padded to ten characters with the spaces on the left.

Thus:You can see the types of values inserted in the substitution markers can vary.

Program that uses padding String: VB.NET

Module Module1
    Sub Main()
	' Format String.
	Dim format As String = "{0,-10} {1,10}"

	' Construct lines.
	Dim line1 As String = String.Format(format, 100, 5)
	Dim line2 As String = String.Format(format, "Carrot", "Giraffe")
	Dim line3 As String = String.Format(format, True, False)

	' Print them.
	Console.WriteLine(line1)
	Console.WriteLine(line2)
	Console.WriteLine(line3)
    End Sub
End Module

Output

100                 5
Carrot        Giraffe
True            False

ToString

String type

You do not always need to use String.Format for formatting patterns. If you have one variable you want to format as a String, you can usually use ToString instead. The substitution markers, such as {0:0000}, can be changed to just 0000.

Note:This is a simpler function call. This simplicity will be reflected in the program's performance.

Program that uses ToString: VB.NET

Module Module1
    Sub Main()
	Dim value As Integer = 123
	Dim a As String = String.Format("{0:0000}", value)
	Dim b As String = value.ToString("0000")

	' Print results. They are equal.
	Console.WriteLine(a)
	Console.WriteLine(b)
    End Sub
End Module

Output

0123
0123

Console.WriteLine

Console screenshot

The examples in this document could be simplified by using the Console.WriteLine method directly with a format string. Console.WriteLine uses the same format patterns as String.Format.

And:You can call the Console.WriteLine function with the same arguments as String.Format.

Console.Write, WriteLine, ReadLine Examples

Summary

The VB.NET programming language

String format patterns can be useful. They can help greatly to decrease the complexity of your string code. Concatenating strings or appending strings can result in more complex statements that are harder to read and fix.


VB.NET: String