1

From the way I understand it and from a number of articles, blogs and threads, I understand that StringBuilder is useful when one has a loop building a string and number of iterations is unknown. Recently I saw StringBuilder used when building a sql string:

StringBuilder sql = new StringBuilder();
sql.Append("SELECT Field1, Field2";
sql.Append(" FROM Table1 WHERE ID=@ID ");

So my question is - does StringBuilder has advantages over a regular String when used in the scenario above? Or it is a matter of personal preference?

Update: My question is not a general String vs. StringBuilder. I just saw a particular case in a text book that did not look particularly advantageous to me so I wanted to clarify with the experts here

3
  • 1
    possible duplicate of String vs. StringBuilder Commented Aug 27, 2015 at 20:03
  • It is not the same thing. I know for sure, because reading the thread you posted di nit help me at all Commented Aug 27, 2015 at 20:05
  • 1
    I don't think it's a good duplicate (of String vs StringBuilder) because this question is focusing in on the - mostly stylistic choice - of how the fixed String query is built. Commented Aug 27, 2015 at 20:11

5 Answers 5

3

A decision to use a StringBuilder requires discretion:

  • If all elements being concatenated are String constants, and all parts are concatenated unconditionally and in the same order, using StringBuilder is a disadvantage.
  • If some elements are added to the result conditionally, StringBuilder may give you a slight advantage.

Specifically, this code

string sql = "SELECT Field1, Field2" +
             " FROM Table1 WHERE ID=@ID ";

is better than the one using StringBuilder, because it does not concatenate strings at all: C# compiler recognizes concatenation of string constants at compile time, and glues the parts into a single string object.

Sign up to request clarification or add additional context in comments.

2 Comments

Explaining what the Java compiler does is great and all, but given that the example is in C#...
@Powerlord You are right. I was looking for a clue to see what language OP uses, but I missed the uppercase A in Append instead of Java's append. Since C# compiler does the same thing, all I needed to do was editing the link. Thanks!
0

In C#, strings are immutable, so each time you add to a string, it creates a new copy. With StringBuilder, it holds the different pieces in memory and then ToString creates it at the end.

Is it advantageous in this scenario, it's difficult to say, because you're only appending two strings together and they are relatively small. The more you join and the bigger the strings get, the faster and less memory StringBuilder will use compared to joining strings together and creating new ones.

2 Comments

...of course, that's overlooking the obvious string sql = "SELECT Field1, Field2 FROM Table1 WHERE ID=@ID" is faster than both of those.
Yes, that's true, but I'm assuming that is a contrived example.
0

Taking your example literally, it would be better to just do

string sql = "SELECT Field1, Field2 FROM Table1 WHERE ID=@ID";

There's really no concatenation needed.

However, assuming that maybe your example doesn't quite get at the heart of your question, basically it comes down to performance vs. readability. In the case of just a few statements like you listed, then who cares about the millisecond performance gain you get from StringBuilder? Nobody, that's who.

But if you are doing many many string concatenations (like in a loop as you mentioned) all those milliseconds will add up and you will notice a difference.

So for the case you gave, just create a simple string. For lots of string manipulation use StringBuilder.

Comments

0

Since you haven't specified the language (both Java and C# have a similar StringBuilder class), I am going to assume it's C#.

Either way, the difference between using string concatenation and StringBuilder will be so minimal in the case you are presenting that I would consider the choice a matter of style.

Personally, if I'm in C#, I find that using verbatim string literals is the cleanest way of building nicely formatted SQL strings:

string sql = @"SELECT Field1, Field2
                 FROM Table1
                WHERE Field1 = @field1
                  AND Field3 = @field3
                ORDER BY Field2";

There is no concatenation involved, and it's very practical when I need to copy paste SQL to and from the code. Sure, there are more spaces in the string, but I usually don't mind that at all, and it doesn't make a difference when running the SQL.

Comments

0

This may help in your choice.

From: https://www.tutorialsteacher.com/csharp/csharp-stringbuilder

"Note : StringBuilder performs faster than string when concatenating multiple strings. Use StringBuilder if you want to append more than three-four string. Appending two or three string is more efficient than using StringBuilder."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.