Kian
Kian

Reputation: 37

C# Is it possible to have arrays be printed indented by a few lines when printed

In a text based game I am making I have menus such as the inventory and player stats set out in a box that looks like this:

    | |====================================| |
    | |                                    | |
    | |                                    | |
    | |====================================| |

I was wondering if it was possible to have it so that an array, in this case the inventory could be printed within this box without overlapping it.

Essentially, what I want to do it indent the printing of the array so that it fits within this box without any issues.

    Console.WriteLine("| | Your inventory contains:                                      | |");
        for (Arraycount = 0; Arraycount < 20; Arraycount++)
        {
            int inventory_position = Arraycount + 1;
            Console.WriteLine("{0}", Inventory[Arraycount]);
        }

Upvotes: 0

Views: 123

Answers (1)

grek40
grek40

Reputation: 13438

You can use a format-string with a specified width and left-alignment:

var items1 = new string[]
{
    "Item 1",
    "Hello World",
    "Here comes trouble aaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb cccccccccccccc dddddddd"
};
Console.WriteLine("| | {0,-60} | |", "Your inventory contains:");
for (int i = 0; i < items1.Length; i++)
{
    Console.WriteLine("| | {0,-60} | |", items1[i]);
}

The "{0,-60}" placeholder tells to format the first argument (0) with a minimum width of 60 and left-aligned (the - before 60).

Just in case you might be tired of counting the right amount of border characters: you can use Console.WriteLine("| |{0}| |", new string('=', 62)); for the upper and lower border that aligns nicely with the width 60 (+2 spaces) contents.

Upvotes: 1

Related Questions