RoadieRich
RoadieRich

Reputation: 6566

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

Upvotes: 6

Views: 32097

Answers (7)

Denis535
Denis535

Reputation: 3590

This should work for \r\n and \n separators.

   public static string Indent(this string value, string indent) {
        var builder = new StringBuilder();
        foreach (var line in value.GetLines()) {
            builder.Append( indent ).Append( line );
        }
        return builder.ToString();
    }
    private static IEnumerable<string> GetLines(this string value) {
        var start = 0;
        while (start < value.Length) {
            var end = value.IndexOf( '\n', start );
            if (end != -1) {
                yield return value.Substring( start, (end - start + 1) );
                start = end + 1;
            } else {
                break;
            }
        }
        if (start < value.Length) yield return value.Substring( start );
    }

Upvotes: 0

Adam Paquette
Adam Paquette

Reputation: 1393

If you want to indent every line of a string you can use this extention.

public static class StringExtensions
{
    public static string Indent(this string value, int size)
    {
        var strArray = value.Split('\n');
        var sb = new StringBuilder();
        foreach (var s in strArray)
            sb.Append(new string(' ', size)).Append(s);
        return sb.ToString();
    }
}

Use it like this :

MyString.Indent(4);

Upvotes: 5

Chris Halcrow
Chris Halcrow

Reputation: 31960

You can use this generic string extension method (it's performance and memory optimal). You can keep it inside a central 'core' project reference by your main application project(s), then whenever you want to get the indented version of any string, it's just:

myString.Indent(n)

(where 'n' is the indent level)

private const byte _indentSize = 4;

public static string Indent(this string originalString, int indentLevel)
{
    StringBuilder indentedString = new StringBuilder();
    indentedString.Append("".PadLeft(indentLevel * _indentSize));
    indentedString.Append(originalString);
    return indentedString.ToString();
}

Upvotes: 1

Sergey Zhukov
Sergey Zhukov

Reputation: 1372

yet another way:

int indentLevel = 4;
string myText = "This string will become indented";

string res = String.Format("{0," + indentLevel + "}{1}", String.Empty, myText);

Upvotes: 0

eandersson
eandersson

Reputation: 26352

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

Upvotes: 11

leppie
leppie

Reputation: 117250

It comes in the box!

Use System.CodeDom.Compiler.IndentedTextWriter.

Upvotes: 5

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174329

You can create your indention with this:

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

IndentSize would be a constant with value 4 or 8.

Upvotes: 25

Related Questions