Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,637 questions

56,925 answers

573 users

How to format a string using fixed-width fields in VB.NET

4 Answers

0 votes
Imports System

Public Class Program
	Public Shared Sub Main()
		Dim str As String = "VB.NET"
		
        Console.Write("{0, 3},{1, 10},{2, 4}", str, "abcd", "123")
    End Sub
End Class



' run:
'
' VB.NET,      abcd, 123
'

 



answered Apr 12, 2025 by avibootz
0 votes
Imports System

Public Class Program
	Public Shared Sub Main()
        Dim fileName As String = "abc.bas"
		
        Dim str As String = String.Format("{0, 3} {1, 8}", fileName, "17MB")
		
        Console.Write(str)
    End Sub
End Class



' run:
'
' abc.bas     17MB
'

 



answered Apr 12, 2025 by avibootz
0 votes
Imports System

' {0,-20} = argument 0, left-aligned, width 20
' {1,8} = argument 1, right-aligned, width 8
' {2,15} = argument 2, right-aligned, width 15

Public Class Program
    Public Shared Sub Main()
        Console.WriteLine("{0,-20}{1,8}{2,15}",
                  "John Smith",
                  45,
                  102500.75D.ToString("C2"))
    End Sub
End Class
 
 

' run:
'
' John Smith                45    $102,500.75
'
 

 



answered 5 days ago by avibootz
0 votes
Imports System

' Key Concepts
' Left-align text: "{0,-20}"
' Right-align text: "{0,10}"
' Negative width = left alignment
' Positive width = right alignment

Module Program

    Sub Main()

        Console.WriteLine("Fixed-Width Field Formatting Demo")
        Console.WriteLine(New String("="c, 55))

        ' Print header row
        Console.WriteLine(
            FormatFixedWidth("Name", 20, FieldAlignment.Left) &
            FormatFixedWidth("Age", 8, FieldAlignment.Right) &
            FormatFixedWidth("Salary", 15, FieldAlignment.Right))

        Console.WriteLine(New String("-"c, 43))

        ' Sample data
        Console.WriteLine(
            FormatFixedWidth("John Smith", 20, FieldAlignment.Left) &
            FormatFixedWidth(35, 8, FieldAlignment.Right) &
            FormatFixedWidth(72500.75D.ToString("C2"), 15, FieldAlignment.Right))

        Console.WriteLine(
            FormatFixedWidth("Mary Johnson", 20, FieldAlignment.Left) &
            FormatFixedWidth(42, 8, FieldAlignment.Right) &
            FormatFixedWidth(98500D.ToString("C2"), 15, FieldAlignment.Right))

        Console.WriteLine(
            FormatFixedWidth("David Lee", 20, FieldAlignment.Left) &
            FormatFixedWidth(29, 8, FieldAlignment.Right) &
            FormatFixedWidth(121500D.ToString("C2"), 15, FieldAlignment.Right))

    End Sub

    ''' <summary>
    ''' Specifies text alignment within a fixed-width field.
    ''' </summary>
    Public Enum FieldAlignment
        Left
        Right
    End Enum

    ''' <summary>
    ''' Formats a string into a fixed-width field.
    ''' If the value exceeds the field width, it is truncated.
    ''' </summary>
    ''' <param name="value">Value to format.</param>
    ''' <param name="width">Field width.</param>
    ''' <param name="alignment">Left or right alignment.</param>
    ''' <returns>Formatted fixed-width string.</returns>
    Public Function FormatFixedWidth(
        value As Object,
        width As Integer,
        alignment As FieldAlignment) As String

        If width <= 0 Then
            Throw New ArgumentOutOfRangeException(
                NameOf(width),
                "Width must be greater than zero.")
        End If

        Dim text As String = If(value?.ToString(), String.Empty)

        ' Prevent overflow into adjacent columns.
        If text.Length > width Then
            text = text.Substring(0, width)
        End If

        Select Case alignment
            Case FieldAlignment.Left
                Return text.PadRight(width)

            Case FieldAlignment.Right
                Return text.PadLeft(width)

            Case Else
                Throw New ArgumentException("Invalid alignment.")
        End Select

    End Function

End Module



' run:
'
' Fixed-Width Field Formatting Demo
' =======================================================
' Name                     Age         Salary
' -------------------------------------------
' John Smith                35     $72,500.75
' Mary Johnson              42     $98,500.00
' David Lee                 29    $121,500.00
' 
  

 



answered 5 days ago by avibootz
...