Coding Duchess
Coding Duchess

Reputation: 6899

Does StringBuilder have an advantage when building from many String literals?

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

Upvotes: 1

Views: 737

Answers (5)

mgp
mgp

Reputation: 57

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."

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 3

sstan
sstan

Reputation: 36473

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.

Upvotes: 0

d512
d512

Reputation: 34083

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.

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115420

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.

Upvotes: 0

Related Questions