none
Assign StringBuilder() to string in same line? RRS feed

  • Question

  • Hello, 

    Is there a way to assign sb to all in this example in one pass without the need to create a StringBuilder object?  I feel like there should and I'm missing something obvious. 

    	StringBuilder sb = new StringBuilder();
    	sb.AppendLine("1");
    	sb.AppendLine("2");
    	sb.AppendLine("3");
    
    	string all = sb.ToString();
    
    //	string all2 = new StringBuilder() {}
    Thanks 

    Wednesday, May 1, 2019 2:58 PM

Answers

  • Check this too:

       string all = string.Join( Environment.NewLine, "1", "2", "3" );

     

    However, it puts new-lines between strings only.



    Wednesday, May 1, 2019 4:25 PM

All replies


  • Is there a way to assign sb to all in this example in one pass without the need to create a StringBuilder object?   

    	StringBuilder sb = new StringBuilder();
    	sb.AppendLine("1");
    	sb.AppendLine("2");
    	sb.AppendLine("3");
    
    	string all = sb.ToString();
    
    //	string all2 = new StringBuilder() {}

    Have you tried these?

    string all2 = new StringBuilder() {}.ToString();
    string all3 = new StringBuilder("Test").ToString();
    

    - Wayne

    Wednesday, May 1, 2019 3:19 PM

  • Have you tried these?

    string all2 = new StringBuilder() {}.ToString();
    string all3 = new StringBuilder("Test").ToString();

    Hi, 

    Works with just one line but not two or more. 

    string all1 = new StringBuilder("1").ToString(); 
    string all2 = new StringBuilder("1","2").ToString(); 

    I guess what I'm struggling to replicate is the three AppendLine from my initial example.  It does work fine in the initial example it's just that niggling feeling that it could be done more cleanly.  



    Wednesday, May 1, 2019 3:50 PM
  • About the only way to get 

    string all2 = new StringBuilder("1","2").ToString(); 

    Is to create a class that inherits from StringBuilder such as

    public class StringBuilderExtended : StringBuilder
    {
    }

    But that will not compile as StringBuilder is a NotInheritable class. The next best thing is to leave well enough alone or write a language extension method e.g.

    public static class StringBuilderExtensions
    {
        public static void AppendArray(this StringBuilder sender, params string[] lines)
        {
            foreach (var line in lines)
            {
                sender.AppendLine(line);
            }
        }
    }

    Usage

    var sb = new StringBuilder();
    sb.AppendArray("Karen", "Anne", "John");

    Your first thought is wait a minute, that's more lines of code than what I want to get away from. But the sense of this is if you plan on doing this in more than one place and perhaps in more than one project, then it makes sense.

    EDIT: Did VB.NET by mistake, changed to C#


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, May 1, 2019 4:25 PM
    Moderator
  • Check this too:

       string all = string.Join( Environment.NewLine, "1", "2", "3" );

     

    However, it puts new-lines between strings only.



    Wednesday, May 1, 2019 4:25 PM
  • Thanks for all the answers.  This works for me just fine. 

    Wednesday, May 1, 2019 4:31 PM

  • Your first thought is wait a minute, that's more lines of code than what I want to get away from. But the sense of this is if you plan on doing this in more than one place and perhaps in more than one project, then it makes sense.

    EDIT: Did VB.NET by mistake, changed to C#

    Thanks for your detailed response.  Good point you make there,  I know I'm sometimes guilty of  trying overhard to perfect something that is only used once.  No worries, I can understand VB.net.  :-) 

    Wednesday, May 1, 2019 4:48 PM

  • Works with just one line but not two or more. 

    string all1 = new StringBuilder("1").ToString(); 
    string all2 = new StringBuilder("1","2").ToString(); 

    I guess what I'm struggling to replicate is the three AppendLine from my initial example. 

    Your second one fails because the 2nd parameter of the overloaded constructor
    must be an int representing the capacity.

    If you want lines:

    string all3 = new StringBuilder("1\r\n" + "2\r\n" + "3\r\n").ToString();
    
    const string CrLf = "\r\n";
    string all4 = new StringBuilder("1" + CrLf + "2" + CrLf + "3" + CrLf).ToString();
    
    

    - Wayne

    Wednesday, May 1, 2019 4:54 PM
  • Using a StringBuilder in cases where there are only a few appends is overkill.
    Why not just create the string directly?

    const string CrLf = "\r\n";
    string str = "1" + CrLf + "2" + CrLf + "3" + CrLf;
    
    

    - Wayne


    Wednesday, May 1, 2019 5:04 PM