Search code examples
vb.netformatcommand-line-interfacemultiple-columnsconsole.writeline

Creating column formatted text with Console.WriteLine()


I am printing a random "card" to the console with `Console.WriteLine()'.

I am trying to print a line with two parts. The first part of the line shows the long-name of the card you have drawn. The second part shows the suit icon and the number: enter image description here

Is there a way to show the second part of the line in a neater/uniform way? Something like this: enter image description here

The problem occurs because the first part of my line changes size depending on the face value and suit. Here is the code I am using to print the card:

Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
Console.Write("     " & suitIcon & " " & textValue)
Console.WriteLine()

I have also tried the following:

 Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
 Dim string2 As String = (suitIcon & " " & textValue)
 Dim padAmount As Integer = 50 - (suit.Length + textValue.Length)
 Console.Write(string2.PadLeft(padAmount, " "c))
 Console.WriteLine()

Which shows the text as: enter image description here


Solution

  • I was able to figure out a solution to my problem with help from @Capellan. I also needed to account for the length of the second part of my string.

    I used to following code to do so:

            Console.Write("Your card is a{0} " & textValue & " of " & suit, IIf(textValue = "Ace", "n", ""))
        Dim string2 As String = (suitIcon & " " & textValue)
        Dim padAmount As Integer = (25 - (suit.Length + textValue.Length)) + suitIcon.Length + textValue.Length
        If textValue = "Ace" Then
            padAmount -= 1
        End If
        Console.Write(string2.PadLeft(padAmount, " "c))
        Console.WriteLine()
    

    This still may not be the best way to do this, so feel free to submit an answer. It did produce the following:

    enter image description here