Search code examples
c#string.formatpadleft

Aligning text into three columns


I would like to show the LinkedList data into three separate columns with List1, List2, and List3 as the column heading. I am unable to align the LinkedList data under these headings.

        void LoopThruLinkedList(LinkedList<string> strList)
        {
            foreach(string str in strList)
            {
                Console.WriteLine(string.Format($"{str, -20}"));
            }
            //Console.WriteLine();
        }

        // Creating a linkedlist
        // Using LinkedList class
        LinkedList<string> my_list = new LinkedList<string>();

        // Adding elements in the LinkedList
        // Using AddLast() method
        my_list.AddLast("Zoya");
        my_list.AddLast("Shilpa");
        my_list.AddLast("Rohit");
        my_list.AddLast("Rohan");
        my_list.AddLast("Juhi");
        my_list.AddLast("Zoya");
        my_list.AddLast("Rohit");

        string List1 = "List One Students: ";
        string List2 = "List Two Students: ";
        string List3 = "List Three Students: ";

        Console.WriteLine($"{List1, -20}{List2, -20}{List3, -20}");


        // Accessing the elements of LinkedList using the foreach loop
        LoopThruLinkedList(my_list);
        LoopThruLinkedList(my_list);
        LoopThruLinkedList(my_list);

Solution

  • There are a lot of issues here

    1. Why do you need linkedList? they have no indexers and makes any solution inefficent
    2. Why are all the lists the same, however maybe this is just an example.
    3. You need to flatten this somehow to write it to the console sequentially

    However this might point you in the right direction

    void LoopThruLinkedList(params LinkedList<string>[] strLists)
    {
       var max = strLists.Max(x => x.Count());
    
       for (int i = 0; i < max; i++)
       {
          foreach (var item in strLists)
             Console.Write($"{(item.Count > i ? item.ElementAt(i) : ""),-20}");
          Console.WriteLine();
       }
    }
    
    // Creating a linkedlist
    // Using LinkedList class
    LinkedList<string> my_list = new LinkedList<string>();
    
    // Adding elements in the LinkedList
    // Using AddLast() method
    my_list.AddLast("Zoya");
    my_list.AddLast("Shilpa");
    my_list.AddLast("Rohit");
    my_list.AddLast("Rohan");
    my_list.AddLast("Juhi");
    my_list.AddLast("Zoya");
    my_list.AddLast("Rohit");
    
    string List1 = "List One Students: ";
    string List2 = "List Two Students: ";
    string List3 = "List Three Students: ";
    
    Console.WriteLine($"{List1,-20}{List2,-20}{List3,-20}");
    
    
    // Accessing the elements of LinkedList using the foreach loop
    LoopThruLinkedList(my_list, my_list, my_list);
    

    Or another variation using enumeration in a pivot style operation with GroupBy

    void LoopThruLinkedList(params LinkedList<string>[] strLists)
    {
       var results = strLists
                    .SelectMany(inner => inner.Select((item, index) => new { item, index }))
                    .GroupBy(i => i.index, i => i.item)
                    .Select(g => g.ToList());
    
       foreach (var list in results)
       {
          foreach (var item in list)
             Console.Write($"{item,-20}");
          Console.WriteLine();
       }
    }