VB.NET Format String

This code snippet shows how to format strings in VB.NET.
  • 3138

You can use the Format method to create formatted strings and concatenate multiple strings representing multiple objects. The Format method automatically converts any passed object into a string.

For example, the following code uses integer, floating number and string values and format them into a string using the Format method.

Listing 1: Using Format method to format a string

    Module Module1
        Sub Main()
            Dim val As Integer = 7
            Dim name As String = "Mr. John"
            Dim num As Single = 45.06F
            Dim str As String = [String].Format("Days Left : {0}. Current DataTime: {1:u}. " & vbLf & " String: {2}, Float: {3}", val, DateTime.Now, name, num)
            Console.WriteLine(str)
            Console.ReadLine()
        End Sub
    End
Module

Output

FS1.gif
 

Conclusion

Hope this article would have helped you in understanding Format Strings in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.