Visual Basic String Format Method

In visual basic, the string Format method is useful to insert the value of variable or an object or expression into another string. By using string Format method, we can replace the format items in the specified string with the string representation of specified objects.

 

Following is the pictorial representation of using string Format() method to insert a variable or expression values into another string in visual basic programming language.

 

Visual basic (vb) String Format Method Representation Diagram

 

If you observe the above diagram, we inserted format items inside of string and replacing those formatted items with the specified string values using Format method.

Visual Basic String Format Method Syntax

Following are the syntaxes of defining a string Format method to replace inserted format items within the string using specified string objects in visual basic programming language.

 

Public Function Format(ByVal _ As String, ByVal _ As Object) As String

Public Function Format(ByVal _ As String, ByVal _ As Object, ByVal _ As Object) As String

Public Function Format(ByVal _ As IFormatProvider, ByVal _ As String, ByVal _ As Object) As String

If you observe the above syntaxes, the first syntax is useful to replace one or more format items in a string with the string representation of the specified object.

 

The second syntax is useful to replace the format items in a string with the string representation of two specified objects and the third syntax is useful to replace the format items in a specified string with the string representation of the corresponding object.

Visual Basic String Format Method Example

Following is the example of using string Format() method to insert an object or variable or expression value into another string in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim s As String = "Name:{0} {1}, Location:{2}, Age:{3}"

        Dim msg As String = String.Format(s, "Suresh", "Dasari", "Hyderabad", 32)

        Console.WriteLine("Format Result: {0}", msg)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used string Format() method to replace a format items in the given string.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual basic (vb) String Format Method Example Result

 

If you observe the above result, the string Format() method has replaced a format items {0} with “Suresh”, {1} with “Dasari”, {2} with “Hyderabad” and {3} with “32” in the given string based on our requirements.

Visual Basic Format String Items

In the above example, we used a string Format() method to replace the format items within the string but by using Format() method we can control the appearance of format items.

 

Following is the example of controlling the appearance of format items using string Format() method in visual basic programming language.

 

Module Module1

    Sub Main()

        Dim num As Decimal = 75.73789621D

        Dim datetime As DateTime = Date.Now

        Console.WriteLine("Format Decimal: {0:n2}", num)

        Console.WriteLine("DateTime: {0}", datetime)

        Console.WriteLine("Only Date: {0:D}", datetime)

        Console.WriteLine("Only Time: {0:T}", datetime)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe above code, we are formatting the given decimal number to only two decimal values using {0:n2} and formatting the datetime value to display only the date {0:d} or time {0:t} based on our requirements.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic (VB) Format String Items using Format Method Example Result

 

If you observe the above result, we formatted the given item values using Format() method based on our requirements.

 

In visual basic, we have a different formats to format the object values using Format() method. Following tables lists the different type of formats available in visual basic programming language.

Visual Basic Date Time Formats

Following table lists the different type of date time formats available in visual basic to format the given datetime values. Generally the Date/Time formats are depend on the user locale so the output may be different.

 

CharacterDescriptionUsageExample
d Short Date {0:d} 29-05-2018
D Long Date {0:D} 29 May 2018
t Short Time {0:t} 05:29:20
T Long Time {0:T} 05:29:20
f or F Long Date Time {0:f} 29 May 2018 05:30:08
g or G Short Date Time {0:g} 29-05-2018 05:31:42
M Short Date {0:M} May 29
r RFC1123 Date Time String {0:r} Tue, 29 May 2018 05:33:02 GMT
s Sortable Date/Time {0:s} 2018-05-29T05:34:10
u Universal Sortable Date {0:u} 2018-05-29 05:35:47Z
U Universal full date {0:U} 29 May 2018 00:08:07
Y Year month pattern {0:Y} May, 2018

Visual Basic Number Formats

Following table lists the different type of number formats available in visual basic to format the given number values. For example, we are passing the decimal value as 75674.73789621.

 

CharacterDescriptionUsageExample
c Currency {0:c} $ 75,674.74
e Scientific {0:e} 7.567474e+004
f Fixed Point {0:f} 75674.74
g General {0:g} 75674.73789621
n Thousand Separator {0:n} 75,674.74

Visual Basic Custom Formats

In visual basic, we can also use the custom formats to format the string. Following table lists the different type of custom formats which we can use to format the strings. For example, we are passing the decimal value as 75674.73789621.

 

CharacterDescriptionUsageExample
0 Zero Placeholder {0:00.00} 75674.74
# Digit Placeholder {0:(#).##} (75674).74
. Decimal Point {0:0.000} 75674.738
, Thousand Separator {0:0,0} 75,675
% Percent {0:0%} 7567474%

This is how we can use the string Format() method to insert format items into the given string and replace those items with their respective values in visual basic programming language.