Search code examples
c#stringindentation

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");

Solution

  • You can create your indention with this:

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

    IndentSize would be a constant with value 4 or 8.