Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

in c# i am making a table in a string, I have this code but it doesn't give me all the…

  1. User

    in c# i am making a table in a string, I have this code but it doesn't give me all the values in the same column, since the first column has a variable size:

    for (int i = 1; i <= rooms; i++) { print += $@" {i}{GetRooms(paidRooms, i),8}{GetRooms2(paidRooms2, i),8}"; }

    the result is a string like this:

    " 9 1 0 10 0 0 "

  2. Assistant

    In order to align the columns properly when creating a table in a string, you can use the String.Format method with a format specifier for each column. Here's an example that aligns the columns properly based on the provided code:

    for (int i = 1; i <= rooms; i++)
    {
        print += string.Format("{0, -10} {1, -10} {2, -10}\n", 
                               i, GetRooms(paidRooms, i), GetRooms2(paidRooms2, i));
    }
    

    In the string.Format method, the format specifier {0, -10} means that the first variable should be left-aligned with a width of 10 characters. Similarly, {1, -10} and {2, -10} are used for the next columns respectively. Adjust the width value as necessary to achieve the desired spacing between columns.