user9945420
user9945420

Reputation:

stringbuilder add initial character to each new line

Currently I have a StringBuilder with some lines

        StringBuilder foo = new StringBuilder()
            .AppendLine("- one")
            .AppendLine("- two")
            .AppendLine("- three");

and I would like to setup the "- " character for each new line. Pseudo code:

        StringBuilder foo = new StringBuilder()
            .SetNewLineInitialCharacter("- ")
            .AppendLine("one")
            .AppendLine("two")
            .AppendLine("three");

I don't think Insert or Replace are methods I am looking for. I know a loop could do the trick but there is no need for it and I'm just asking if there is a way setting up "- " for one single time.

Upvotes: 2

Views: 493

Answers (3)

Anastasios Selmani
Anastasios Selmani

Reputation: 3699

A simple solution could be something like that

public class CustomStringBuilder 
{
    private StringBuilder _builder;
    private string _prefix;

    public CustomStringBuilder (StringBuilder builder, string prefix)
    {
            _builder = builder;
            _prefix = prefix;
    }

    public CustomStringBuilder CustomAppendLine(string text)
    {
        _builder .Append(_prefix);
        _builder .AppendLine(text);
        return this;
    }

    public override string ToString()
    {
        return _builder.ToString(); 
    }
}

and you could call it like that:

CustomStringBuilderfoo = new CustomStringBuilder(new StringBuilder(), "- ")
            .CustomAppendLine ("one")
            .CustomAppendLine ("two")
            .CustomAppendLine ("three");

Upvotes: 1

Magnus
Magnus

Reputation: 46947

One of many ways to do this would be to wrap the StringBuilder in a separate class.

void Main()
{
    var sb = new StringBuilder();
    var foo = new MyStringBuilder(sb, "- ")
            .AppendLine("one")
            .AppendLine("two")
            .AppendLine("three");

    var result = sb.ToString();
}

public class MyStringBuilder
{
    private StringBuilder _sb;
    private string _linePrefix;

    public MyStringBuilder(StringBuilder sb, string linePrefix)
    {
            _sb = sb;
            _linePrefix = linePrefix;
    }

    public MyStringBuilder AppendLine(string line)
    {
        _sb.Append(_linePrefix);
        _sb.AppendLine(line);
        return this;
    }

    public override string ToString()
    {   
       return _sb.ToString(); 
    }
}

Upvotes: 4

CodeCaster
CodeCaster

Reputation: 151604

You can do this using a wrapped StringBuilder:

public class StringBuilderWrapper
{
    private readonly string _prefix;
    private readonly StringBuilder _builder;

    public StringBuilderWrapper(StringBuilder builder, string prefix)
    {
        _prefix = prefix;
        _builder = builder;
    }

    public StringBuilderWrapper AppendLine(string line)
    {
        _builder.Append(_prefix);
        _builder.AppendLine(line);

        return this;
    }

    public override string ToString()
    {
        return _builder.ToString(); 
    }
}

Which, for convenience, you can return from an extension method:

public static class StringBuilderExtensions
{
    public static StringBuilderWrapper SetNewLineInitialCharacter(this StringBuilder builder, string prefix)
    {
        return new StringBuilderWrapper(builder, prefix);
    }
}

Then call it like this:

var output = new StringBuilder()
    .SetNewLineInitialCharacter("- ")
    .AppendLine("one")
    .AppendLine("two")
    .AppendLine("three");

var outputString = output.ToString();

Which outputs:

- one
- two
- three

Without the extension method, you'd call it like this:

var output = new StringBuilderWrapper(new StringBuilder(), "- ")
    .AppendLine("one")
    .AppendLine("two")
    .AppendLine("three");

Upvotes: 5

Related Questions