Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I concatenate strings?

Are there differences between these examples? Which should I use in which case?

var str1 = "abc" + dynamicString + dynamicString2;

var str2 = String.Format("abc{0}{1}", dynamicString, dynamicString2);

var str3 = new StringBuilder("abc").
    Append(dynamicString).
    Append(dynamicString2).
    ToString();

var str4 = String.Concat("abc", dynamicString, dynamicString2);

There are similar questions:

  • Difference in String concatenation which only asks about the + operator, and it's not even mentioned in the answer that it is converted to String.Concat
  • What's the best string concatenation method which is not really related to my question, where it is asking for the best, and not a comparation of the possible ways to concatenate a string and their outputs, as this question does.

This question is asking about what happens in each case, what will be the real output of those examples? What are the differences about them? Where should I use them in which case?

like image 954
BrunoLM Avatar asked Jun 23 '10 14:06

BrunoLM


People also ask

What is the best way to concatenate strings in Python?

One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

What is the best way to concatenate strings in Java?

Using + Operator The + operator is one of the easiest ways to concatenate two strings in Java that is used by the vast majority of Java developers. We can also use it to concatenate the string with other data types such as an integer, long, etc.

What is string concatenation example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.


2 Answers

As long as you are not deailing with very many (100+) strings or with very large (Length > 10000) strings, the only criterion is readability.

For problems of this size, use the +. That + overload was added to the string class for readability.

Use string.Format() for more complicated compositions and when substitutions or formatting are required.

Use a StringBuilder when combining many pieces (hundreds or more) or very large pieces (length >> 1000). StringBuilder has no readability features, it's just there for performance.

like image 132
Henk Holterman Avatar answered Oct 07 '22 22:10

Henk Holterman


Gathering information from all the answers it turns out to behave like this:

The + operator is the same as the String.Concat, this could be used on small concatenations outside a loop, can be used on small tasks.

In compilation time, the + operator generate a single string if they are static, while the String.Concat generates the expression str = str1 + str2; even if they are static.

String.Format is the same as StringBuilder.. (example 3) except that the String.Format does a validation of params and instantiate the internal StringBuilder with the length of the parameters.

String.Format should be used when format string is needed, and to concat simple strings.

StringBuilder should be used when you need to concatenate big strings or in a loop.

like image 33
BrunoLM Avatar answered Oct 07 '22 20:10

BrunoLM