-8

Hi guys I am new to C# programming and I wanted to print a tree table like textual information

eg:
City
  Munich
  London
Country
  UK
  IND

how can I print this information using the stringbuilder? it need not be a high end UI design just some textual information using indentation.

Edit: The information actually expected in the format is:

Patient Name   Test
ABC            Cardiology
                 ECG

and this information is iterated inside a foreach loop using StringBuilder

Mr. A
  • 103
  • 1
  • 2
  • 17
  • 4
    Please, provide your attempts (code) – Dmitry Bychenko Dec 16 '19 at 10:11
  • 1
    What's the target? A Console app? Web page? Forms app? Something else? You should just be able to add some newlines and tabs (or spaces), if it's a simple console application. What have you tried? Where are you stuck? – ADyson Dec 16 '19 at 10:12
  • @ADyson target is console app – Mr. A Dec 16 '19 at 10:12
  • 2
    It will be cool to have a [mre], At least the Object source type. Btw for Tree you can use ascii char like `├── Foo/ │ ├── b │ └── b` – xdtTransform Dec 16 '19 at 10:13
  • 1
    Ok...so what have you tried? Where are you stuck? Can we see how you are building the string right now? – ADyson Dec 16 '19 at 10:16
  • Take [this](https://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure) as an example and change slightly. – Gleb Dec 16 '19 at 10:18
  • @Gleb, nice dupe target! – xdtTransform Dec 16 '19 at 10:22
  • Does this answer your question? [How do I print out a tree structure?](https://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure) – xdtTransform Dec 16 '19 at 10:22
  • 2
    Duplicate doesn't act in ask restriction. Poor question does. Read the linked article go back to your question and fix them. The system is automated no amount of ping to user will fix it for you. – xdtTransform Dec 16 '19 at 12:26

1 Answers1

4

you can use the escape caracters \n for the new line, and \t for the tab.

You can also use the method AppendLine not to use the \n.

To get what you want, you would do:

var sb = new System.Text.StringBuilder();
sb.AppendLine("City");
sb.AppendLine("\tMunich");
sb.AppendLine("\tLondon");
sb.AppendLine("Country");
sb.AppendLine("\tUK");
sb.AppendLine("\tIND");
Console.Write(sb);

or this:

var sb = new System.Text.StringBuilder();
sb.Append("City\n\tMunich\n\tLondon\nCountry\n\tUK\n\tIND\n");
Console.Write(sb);

a complete generic version does the trick: https://dotnetfiddle.net/qhjCsJ

using System;
using System.Collections.Generic;
using System.Text;

public class Node
{
    public string Text {get;set;}
    public IEnumerable<Node> Children{get;set;}=new List<Node>();
}

public class Program
{
    public static void Main()
    {
        var nodes=new []{
            new Node{
                Text="City",
                Children=new []{
                    new Node{Text="Munich"},
                    new Node{Text="London"},
                }
            },
            new Node{
                Text="Country",
                Children=new []{
                    new Node{Text="UK"},
                    new Node{Text="IND"},
                }
            }
        };

        var sb = new System.Text.StringBuilder();
        foreach(var node in nodes)
            RenderNode(sb, node);
        Console.Write(sb);
    }
    private static void RenderNode(StringBuilder sb, Node node, int indentationLevel = 0){
        sb.AppendLine(new String('\t', indentationLevel) + node.Text);
        foreach(var child in node.Children)
            RenderNode(sb, child, indentationLevel+1);
    }
}
Stephane
  • 1,359
  • 1
  • 15
  • 27
  • every item is added as appendLine() that will shuffle the formatting – Mr. A Dec 16 '19 at 10:14
  • seems to work pretty fine here: https://dotnetfiddle.net/8EXlwo – Stephane Dec 16 '19 at 10:19
  • yeah it is working fine but if I want to iterate it the the same information will repeat inside the loop. Actually I want to iterate it inside a loop – Mr. A Dec 16 '19 at 10:25
  • 3
    This is a perfect example of why you should provide code before you ask a question, otherwise the ones who tries to help have to guess what you're trying to accomplish. – Joel Wiklund Dec 16 '19 at 10:28
  • And please upvote this question it seems due to down votes I am restricted from asking a new question – Mr. A Dec 16 '19 at 10:35
  • I'm not a fan. Second part look like `var str= "copypast your string ";` and the first part is just `AppendLine`, `\t`, and `\n`. I don't expect Tab and new line symbole to be that usefull. – xdtTransform Dec 16 '19 at 10:36
  • 2
    @Mr.A , You may find the information relative to this restriction [here](https://meta.stackoverflow.com/questions/255583/) . As I said on the question you may want to improve the question by [editing](https://stackoverflow.com/posts/59354417/edit) an [mre]. We don't need mutch: The class declaration of the Item you are looping on. And the initialisation of a collection/list/array of item that hold the example information. a simplification of your actual problem may be about 10-20 lines of codes. – xdtTransform Dec 16 '19 at 10:38
  • @Stephane, sorry for the off topics comment, but OP Asked for upvoted needed to be addressed. – xdtTransform Dec 16 '19 at 10:43
  • @xdtTransform no pb ;) – Stephane Dec 16 '19 at 10:46
  • @ASh can you please unmark this question as duplicate as I am not able to ask a new question due to the restrictions imposed – Mr. A Dec 16 '19 at 11:09
  • @Owen Pauling can you please unmark this question as duplicate as I am not able to ask a new question due to the restrictions imposed – Mr. A Dec 16 '19 at 11:09
  • 1
    @Mr.A have you been listening to xdtTransform? The fact your question is a duplicate is **not** the reason why you cannot ask new questions. Please take a moment to read and understand this article: https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th . The number of downvotes you have received is likely to be the main reason why you are banned from asking new questions. Duplicates are ok. Poor-quality questions are not. Downvotes are given for poor-quality questions, such as this one. – ADyson Dec 16 '19 at 14:40
  • @ADyson what is wrong with this question which made it a poor quality question? – Mr. A Dec 17 '19 at 06:13
  • 1
    You haven't shown what you've tried to solve the problem yourself, even after being prompted to do so in the very first comment. It might be a good idea to take a look at [ask] to help you improve your questions on this site. – Frauke Dec 17 '19 at 11:00
  • 1
    @Mr.A No code attempt, no apparent attempt at any basic research (just googling your question title would probably have got you some clues!), not even a basic description of any problem you faced when trying to implement. All you did was ask someone else to solve the problem for you. That's generally not our job, unless it's very trivial. Most people won't help you with (what looks like) a no-effort question. Also, you can hover over the downvote button on any question to see the standard reasons why downvotes can be given. – ADyson Dec 17 '19 at 12:41