Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating an indented string for a single line of text

What is the best way to generate an indented line from space characters. I mean something similar to this:

    string indent = String.Join("    ", new String[indentlevel]);
    s.Write(indent + "My line of text");
like image 654
RoadieRich Avatar asked Mar 20 '13 16:03

RoadieRich


2 Answers

You can create your indention with this:

var indent = new string(' ', indentLevel * IndentSize);

IndentSize would be a constant with value 4 or 8.

like image 125
Daniel Hilgarth Avatar answered Oct 13 '22 00:10

Daniel Hilgarth


I would probably do something like this to add Indent.

public static string Indent(int count)
{
    return "".PadLeft(count);
}

To use it you can do the following:

Indent(4) + "My Random Text"

In your application you could simply do:

s.Write(Indent(indentLevel));

or

s.Write("".PadLeft(indentLevel));
like image 25
eandersson Avatar answered Oct 12 '22 22:10

eandersson