How to use String.Format with multiple options in VB.NET

1 Answer

0 votes
Imports System
Imports System.Globalization

Module Program
    Sub Main()

        Console.WriteLine("=== BASIC PLACEHOLDERS ===")
        Dim name As String = "Daisy"
        Dim age As Integer = 27
        Console.WriteLine(String.Format("My name is {0} and I am {1} years old.", name, age))

		Console.WriteLine(Environment.NewLine & "=== NUMBER FORMATTING ===")
        Dim price As Double = 1234.56789
        Console.WriteLine(String.Format("Fixed-point: {0:F2}", price))
        Console.WriteLine(String.Format("Number with commas: {0:N2}", price))
        Console.WriteLine(String.Format("Currency: {0:C}", price))
        Console.WriteLine(String.Format("Percent: {0:P1}", 0.985))

        Console.WriteLine(Environment.NewLine & "=== ALIGNMENT ===")
        Console.WriteLine(String.Format("|{0,10}|", "Tessa"))   ' right align
        Console.WriteLine(String.Format("|{0,-10}|", "Tessa"))  ' left align

        Console.WriteLine(Environment.NewLine & "=== CUSTOM NUMERIC FORMATS ===")
        Console.WriteLine(String.Format("Custom: {0:00000.00}", price))
        Console.WriteLine(String.Format("Hex: {0:X}", 255))

        Console.WriteLine(Environment.NewLine & "=== DATE/TIME FORMATTING ===")
        Dim now As DateTime = DateTime.Now
        Console.WriteLine(String.Format("Short date: {0:d}", now))
        Console.WriteLine(String.Format("Long date: {0:D}", now))
        Console.WriteLine(String.Format("ISO: {0:yyyy-MM-dd HH:mm:ss}", now))

        Console.WriteLine(Environment.NewLine & "=== CULTURE-SPECIFIC FORMATTING ===")
        Dim ro As New CultureInfo("ro-RO")
        Console.WriteLine(String.Format(ro, "Currency (RO): {0:C}", price))

        Console.WriteLine(Environment.NewLine & "=== NESTED EXPRESSIONS ===")
        Console.WriteLine(String.Format("Uppercase: {0}", name.ToUpper()))

        Console.WriteLine(Environment.NewLine & "=== INDEXED ARGUMENTS (REORDERING) ===")
        Console.WriteLine(String.Format("Second: {1}, First: {0}", "Isla", "Zoe"))

        Console.WriteLine(Environment.NewLine & "=== ESCAPING BRACES ===")
        Console.WriteLine(String.Format("Use {{ and }} to escape braces: {{0}}"))

    End Sub
End Module



' run:
'
' === BASIC PLACEHOLDERS ===
' My name is Daisy and I am 27 years old.
' 
' === NUMBER FORMATTING ===
' Fixed-point: 1234.57
' Number with commas: 1,234.57
' Currency: $1,234.57
' Percent: 98.5 %
' 
' === ALIGNMENT ===
' |     Tessa|
' |Tessa     |
' 
' === CUSTOM NUMERIC FORMATS ===
' Custom: 01234.57
' Hex: FF
' 
' === DATE/TIME FORMATTING ===
' Short date: 5/9/2026
' Long date: Saturday, May 9, 2026
' ISO: 2026-05-09 15:16:11
' 
' === CULTURE-SPECIFIC FORMATTING ===
' Currency (RO): 1.234,57 RON
' 
' === NESTED EXPRESSIONS ===
' Uppercase: DAISY
' 
' === INDEXED ARGUMENTS (REORDERING) ===
' Second: Zoe, First: Isla
' 
' === ESCAPING BRACES ===
' Use { and } to escape braces: {0}
' 

 



answered May 9 by avibootz
edited May 9 by avibootz
...