VB.NET String.Format() method

String.Format( ) method is used to combine textual strings and inserted data with different formatting option. Stirng.Format( ) method requires many argument.
  • 8947
 

String.Format( ) method is a static method of String Class. It is used to combine textual strings and inserted data with different formatting option. Stirng.Format( ) method requires many argument. In the string.format( ) method string itself is the first argument. The position of inserted argument start from left of the colon with index value like "0", "1:" and "2:". The index value indicate number of argument are used within {} bracket and it is used to refer other parameters passed to the format method. Index number start form 0 integer. Bracket {} indicate the proper format for variable.

string-format1.GIF

In this example , I have used three String.Format() method. First method used two argument, second method used three argument with different format method like show value in decimal using (0.0) and present year in yyyy format, and in the last method integer convert into decimal. To know its funtionallity how its display result use the below example and see output in below image.

Module Module1
    Sub Main()
       Console.WriteLine(String.Format("There are about {0} minutes in {1} one day", 60 * 24, 1))

        Dim value1 As Integer = 8
        Dim str1 As String = "November"
        Dim dat1 As DateTime = New DateTime(1987, 8, 11)
        ' Use string.Format method with four arguments.
        ' The first argument is the formatting string.
        ' It specifies how the next three arguments are formatted that wiil show date .
        Dim result As String = String.Format("{0}: {1:0.0} : {2:yyyy}", value1, str1, dat1)
        ' Write the result.
        Console.WriteLine(result)

        Dim value As Integer = 100
        ' Convert a number to decimal
        Console.WriteLine("The interger value {0} = {0:0.00} decimal.", value)
        Console.ReadLine()

    End Sub
End Module

Output

string-format.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.