stringbuilder add initial character to each new line





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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.










share|improve this question


















  • 3





    Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

    – Panagiotis Kanavos
    Nov 22 '18 at 11:21








  • 3





    You can create your own class of stringbuilder inheritance from stringbuilder and additional options

    – Kaspar
    Nov 22 '18 at 11:23











  • You could also write an extension method for StringBuilder

    – cmos
    Nov 22 '18 at 11:25











  • StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

    – Panagiotis Kanavos
    Nov 22 '18 at 11:28






  • 1





    @Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

    – Panagiotis Kanavos
    Nov 22 '18 at 11:29


















1















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.










share|improve this question


















  • 3





    Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

    – Panagiotis Kanavos
    Nov 22 '18 at 11:21








  • 3





    You can create your own class of stringbuilder inheritance from stringbuilder and additional options

    – Kaspar
    Nov 22 '18 at 11:23











  • You could also write an extension method for StringBuilder

    – cmos
    Nov 22 '18 at 11:25











  • StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

    – Panagiotis Kanavos
    Nov 22 '18 at 11:28






  • 1





    @Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

    – Panagiotis Kanavos
    Nov 22 '18 at 11:29














1












1








1








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.










share|improve this question














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.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 11:19









MHComputechMHComputech

131114




131114








  • 3





    Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

    – Panagiotis Kanavos
    Nov 22 '18 at 11:21








  • 3





    You can create your own class of stringbuilder inheritance from stringbuilder and additional options

    – Kaspar
    Nov 22 '18 at 11:23











  • You could also write an extension method for StringBuilder

    – cmos
    Nov 22 '18 at 11:25











  • StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

    – Panagiotis Kanavos
    Nov 22 '18 at 11:28






  • 1





    @Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

    – Panagiotis Kanavos
    Nov 22 '18 at 11:29














  • 3





    Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

    – Panagiotis Kanavos
    Nov 22 '18 at 11:21








  • 3





    You can create your own class of stringbuilder inheritance from stringbuilder and additional options

    – Kaspar
    Nov 22 '18 at 11:23











  • You could also write an extension method for StringBuilder

    – cmos
    Nov 22 '18 at 11:25











  • StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

    – Panagiotis Kanavos
    Nov 22 '18 at 11:28






  • 1





    @Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

    – Panagiotis Kanavos
    Nov 22 '18 at 11:29








3




3





Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

– Panagiotis Kanavos
Nov 22 '18 at 11:21







Just add the character on each call. Or use AppendFormat, eg AppendFormat("- {0}",whatever). Even AppendLine($"- {whatever}")

– Panagiotis Kanavos
Nov 22 '18 at 11:21






3




3





You can create your own class of stringbuilder inheritance from stringbuilder and additional options

– Kaspar
Nov 22 '18 at 11:23





You can create your own class of stringbuilder inheritance from stringbuilder and additional options

– Kaspar
Nov 22 '18 at 11:23













You could also write an extension method for StringBuilder

– cmos
Nov 22 '18 at 11:25





You could also write an extension method for StringBuilder

– cmos
Nov 22 '18 at 11:25













StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

– Panagiotis Kanavos
Nov 22 '18 at 11:28





StringBuilder's job is to create one big string without generating temporary strings and excessive reallocations. You are asking for something that will generate a specific layout.

– Panagiotis Kanavos
Nov 22 '18 at 11:28




1




1





@Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

– Panagiotis Kanavos
Nov 22 '18 at 11:29





@Kaspar that would be a bad idea - the result wouldn't only try to do two different things (generate strings and layouts), it would also break the expected behaviour of the base class. It's impossible though because StringBuilder is sealed

– Panagiotis Kanavos
Nov 22 '18 at 11:29












3 Answers
3






active

oldest

votes


















4














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





share|improve this answer

































    5














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





    share|improve this answer


























    • Aren't you copy-pasted your answer from @CodeCaster's one?

      – vasily.sib
      Nov 22 '18 at 11:43











    • @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

      – Magnus
      Nov 22 '18 at 11:45













    • CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

      – vasily.sib
      Nov 22 '18 at 11:50






    • 1





      @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

      – CodeCaster
      Nov 22 '18 at 11:52



















    2














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





    share|improve this answer





















    • 1





      @John well you fixed everyone's answer... :) thanks a lot!

      – Anastasios Selmanis
      Nov 22 '18 at 11:45






    • 1





      Well, everyone beat me to giving one so it's the least I could do lol

      – John
      Nov 22 '18 at 11:46












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429820%2fstringbuilder-add-initial-character-to-each-new-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














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





    share|improve this answer






























      4














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





      share|improve this answer




























        4












        4








        4







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





        share|improve this answer















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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 11:40

























        answered Nov 22 '18 at 11:31









        CodeCasterCodeCaster

        110k17148200




        110k17148200

























            5














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





            share|improve this answer


























            • Aren't you copy-pasted your answer from @CodeCaster's one?

              – vasily.sib
              Nov 22 '18 at 11:43











            • @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

              – Magnus
              Nov 22 '18 at 11:45













            • CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

              – vasily.sib
              Nov 22 '18 at 11:50






            • 1





              @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

              – CodeCaster
              Nov 22 '18 at 11:52
















            5














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





            share|improve this answer


























            • Aren't you copy-pasted your answer from @CodeCaster's one?

              – vasily.sib
              Nov 22 '18 at 11:43











            • @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

              – Magnus
              Nov 22 '18 at 11:45













            • CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

              – vasily.sib
              Nov 22 '18 at 11:50






            • 1





              @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

              – CodeCaster
              Nov 22 '18 at 11:52














            5












            5








            5







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 11:54

























            answered Nov 22 '18 at 11:34









            MagnusMagnus

            36.6k75591




            36.6k75591













            • Aren't you copy-pasted your answer from @CodeCaster's one?

              – vasily.sib
              Nov 22 '18 at 11:43











            • @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

              – Magnus
              Nov 22 '18 at 11:45













            • CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

              – vasily.sib
              Nov 22 '18 at 11:50






            • 1





              @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

              – CodeCaster
              Nov 22 '18 at 11:52



















            • Aren't you copy-pasted your answer from @CodeCaster's one?

              – vasily.sib
              Nov 22 '18 at 11:43











            • @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

              – Magnus
              Nov 22 '18 at 11:45













            • CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

              – vasily.sib
              Nov 22 '18 at 11:50






            • 1





              @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

              – CodeCaster
              Nov 22 '18 at 11:52

















            Aren't you copy-pasted your answer from @CodeCaster's one?

            – vasily.sib
            Nov 22 '18 at 11:43





            Aren't you copy-pasted your answer from @CodeCaster's one?

            – vasily.sib
            Nov 22 '18 at 11:43













            @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

            – Magnus
            Nov 22 '18 at 11:45







            @vasily.sib Or is he a copy of mine? ;-) When I wrote this CodeCaster's answer did not look as it does now.

            – Magnus
            Nov 22 '18 at 11:45















            CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

            – vasily.sib
            Nov 22 '18 at 11:50





            CodeCaster's revision#2 at 2018-11-22 11:32:39Z Your revision#1 at 2018-11-22 11:34:45Z you are almost 2 minutes late

            – vasily.sib
            Nov 22 '18 at 11:50




            1




            1





            @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

            – CodeCaster
            Nov 22 '18 at 11:52





            @vasily.sib no plagiarism from my side and I don't suspect Magnus either, this "adapter pattern" is a pretty common pattern to add functionality to a sealed class.

            – CodeCaster
            Nov 22 '18 at 11:52











            2














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





            share|improve this answer





















            • 1





              @John well you fixed everyone's answer... :) thanks a lot!

              – Anastasios Selmanis
              Nov 22 '18 at 11:45






            • 1





              Well, everyone beat me to giving one so it's the least I could do lol

              – John
              Nov 22 '18 at 11:46
















            2














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





            share|improve this answer





















            • 1





              @John well you fixed everyone's answer... :) thanks a lot!

              – Anastasios Selmanis
              Nov 22 '18 at 11:45






            • 1





              Well, everyone beat me to giving one so it's the least I could do lol

              – John
              Nov 22 '18 at 11:46














            2












            2








            2







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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 7 at 12:16









            smart_dude

            33




            33










            answered Nov 22 '18 at 11:29









            Anastasios SelmanisAnastasios Selmanis

            2,05431735




            2,05431735








            • 1





              @John well you fixed everyone's answer... :) thanks a lot!

              – Anastasios Selmanis
              Nov 22 '18 at 11:45






            • 1





              Well, everyone beat me to giving one so it's the least I could do lol

              – John
              Nov 22 '18 at 11:46














            • 1





              @John well you fixed everyone's answer... :) thanks a lot!

              – Anastasios Selmanis
              Nov 22 '18 at 11:45






            • 1





              Well, everyone beat me to giving one so it's the least I could do lol

              – John
              Nov 22 '18 at 11:46








            1




            1





            @John well you fixed everyone's answer... :) thanks a lot!

            – Anastasios Selmanis
            Nov 22 '18 at 11:45





            @John well you fixed everyone's answer... :) thanks a lot!

            – Anastasios Selmanis
            Nov 22 '18 at 11:45




            1




            1





            Well, everyone beat me to giving one so it's the least I could do lol

            – John
            Nov 22 '18 at 11:46





            Well, everyone beat me to giving one so it's the least I could do lol

            – John
            Nov 22 '18 at 11:46


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429820%2fstringbuilder-add-initial-character-to-each-new-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?