C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It is possible to concatenate two or more strings with several syntax forms. We use the plus operator and the string.Concat method. The plus compiles into string.Concat.
Tip: The main reason to use "+" instead of string.Concat is readability. Both expressions will compile into the same code.
Example. First, we concatenate two strings with the plus sign. We then use the string.Concat method. Internally, the C# compiler converts the plus operator into string.Concat. So only the syntax is different.
Here: We concat a string literal ("string1") with a string variable (s1). The reference is stored in the s2 local.
C# program that concatenates strings using System; class Program { static void Main() { // ... Create a new string reference. // It points to the literal. string s1 = "string2"; // ... Add another string to the start. string s2 = "string1" + s1; Console.WriteLine(s2); } } Output string1string2
Next, the performance of combining two strings with plus or string.Concat is excellent. Using other methods, such as StringBuilder, for just two strings is slower. Here's the equivalent code with string.Concat.
C# program that uses string.Concat method using System; class Program { static void Main() { // 1. string s1 = "string2"; // 2. string s2 = string.Concat("string1", s1); // 3. Console.WriteLine(s2); } } Output string1string2
Three strings. For three strings, we use the same string.Concat or plus operator. My testing shows this has good performance and is simpler than other methods such as string.Format. Concat with three arguments works equally well here.
Note: You can use the + in any order, just like method arguments—which essentially is what the operands become.
C# program that concats three strings using System; class Program { static void Main() { // 1. string s1 = "string1"; // 2. string s2 = "string2"; // 3. // Combine three strings. string s3 = s1 + s2 + "string3"; // 4. // Write the result to the screen. Console.WriteLine(s3); } } Output string1string2string3
Add to start. Adding to the start is called prepending. Performance here is good. Which method to use depends on what you find clearer. You can append strings by using + or string.Concat. But for many appends in a row, consider StringBuilder.
Four strings. To combine four strings, you can use the same syntax above. Here we start seeing performance changes. For four strings, it is fastest to add them all in a single statement, with string.Concat or the plus operator.
The string.Format method also becomes more usable appending many strings here. But it has worse performance. We start considering StringBuilder when multiple strings are encountered. It also has much worse performance at this level.
C# program that concats four strings using System; using System.Text; class Program { static void Main() { string s1 = "string1"; // A. // Concat 4 strings, each in a separate statement. { string s2 = "string1" + s1; s2 += "string2"; s2 += s1; Console.WriteLine(s2); } // B. // Concat 4 strings, in one statement. { string s2 = string.Concat("string1", s1, "string2", s1); Console.WriteLine(s2); } // C. // Concat 4 strings using a format string. { string s2 = string.Format("string1{0}string2{0}", s1); Console.WriteLine(s2); } // D. // Concat 4 strings, three at a time then one. { string s2 = "string1" + s1 + "string2"; s2 += s1; Console.WriteLine(s2); } // E. // Concat 4 strings, one at a time with StringBuilder. { string s2 = new StringBuilder("string1").Append( s1).Append("string2").Append(s1).ToString(); Console.WriteLine(s2); } Console.ReadLine(); } } Output string1string1string2string1 string1string1string2string1 string1string1string2string1 string1string1string2string1 string1string1string2string1
Benchmark. What I display here is the benchmarks for four strings combined into one. For fewer than four strings, using string.Concat or + is fast and also clearest to read. But there are more options for four strings.
I compared the methods above. I had to write several programs to benchmark them. To prevent the compiler from solving the program before it is run, I built up one of the strings at runtime.
So: At four strings of short lengths, using string.Concat or the plus operator + on all the strings in one statement is fastest.
String Concat performance: 4 strings A: 557 ms (Concat 4 strings, 1 at a time) B: 281 ms (Concat 4 strings, all at once) C: 1342 ms (Concat 4 strings with a format string) D: 421 ms (Concat 4 strings, 3 at a time and then 1) E: 812 ms (Concat 4 strings, 1 at a time with StringBuilder)
Five strings. We can concat five strings with the same methods as shown above. Here the performance radically changes. Looking into the intermediate language, we see a new overload of string.Concat is being used.
C# program that concatenates five strings using System; using System.Text; class Program { static void Main() { string s1 = "string1"; // A. // Concat 5 strings in two statements. { string s2 = s1 + "string1" + s1 + "string2"; s2 += s1; Console.WriteLine(s2); } // B. // Concat 5 strings in one statement. { string s2 = s1 + "string1" + s1 + "string2" + s1; Console.WriteLine(s2); } // C. // Concat 5 strings in StringBuilder. { string s2 = new StringBuilder(s1).Append( "string1").Append(s1).Append( "string2").Append(s1).ToString(); Console.WriteLine(s2); } Console.ReadLine(); } } Output string1string1string1string2string1 string1string1string1string2string1 string1string1string1string2string1
Part A uses two statements to combine the strings. Part B uses a single statement. And part C uses StringBuilder. I was surprised to find that part A is the fastest. Using string.Concat on all strings at once is suddenly not the best.
Why is concatenating five strings different? In the .NET Framework, there are string.Concat overloaded methods that accept between two and four parameters. Finally, there is an override that accepts an array.
And: When you concat five strings, the array overload method is used. Looking into IL Disassembler, we see a list of many Concat methods.
However: There isn't one with five arguments. When five arguments are needed, the params version is used.
Internal implementation. Looking even deeper, I saw that the internals use ConcatArray, which seems to have a much different implementation. Unfortunately, this overload doesn't perform as well as the ones with fewer parameters.
Benchmark 2. It is fastest to first concat four strings, then concat that with more strings. Instead of combining all strings at once, it is faster to combine four in a statement. This won't result in needing the ConcatArray internal method.
Note: I feel understanding exactly how strings are concatenated is extremely useful.
And: The performance difference between four concats at once and five concats at once is relevant.
String Concat performance: 5 strings A: 484 ms (Concat 5 strings in two statements) B: 686 ms (Concat 5 strings in one statement) C: 889 ms (Concat 5 strings in StringBuilder)
Guidelines. Here are my guidelines. For fewer than five strings, always use one statement. This statement should directly use string.Concat or the + operator. But for five or more strings, use multiple statements of four strings at once.
Note: This is appropriate for when you have a known number of strings, not an unknown or perhaps large number of strings.
Performance degradation. Further, if you have a loop or could have many more than five strings, use StringBuilder. This may perform worse sometimes, but it will prevent edge cases of huge numbers of strings from causing problems.
Also: MSDN provides information on the string.Concat overloads. Look at the links to the methods that accept arrays and those that don't.
List. We create a List instance and add three string literals to it. In new versions of the .NET Framework, you can pass the List variable reference to the string.Concat method. It will concatenate all the strings with no separator.
Note: The string.Concat method here is equivalent to the string.Join method invocation with an empty separator.
C# program that concats string list using System; using System.Collections.Generic; class Program { static void Main() { // Create list of three strings. var list = new List<string>(); list.Add("cat"); list.Add("dog"); list.Add("deves"); // Concat the list. string concat = string.Concat(list); Console.WriteLine(concat); // Join the list. string join = string.Join("", list); Console.WriteLine(join); } } Output catdogdeves catdogdeves
What's the difference? Which of the two versions of the code, string.Concat or string.Join, should you use? As it turns out, the implementations in the .NET Framework 4.0 are the same except string.Join repeatedly appends the separator.
Note: Appending an empty string is fast, but not doing so is even faster, so the string.Concat method would be superior here.
Tip: The string.Concat method in the .NET Framework 4.0 has an overload that receives an IEnumerable collection of type string.
IEnumerable Examples: LINQ, Lists and Arrays
Summary. Strings are frequently combined (concatenated). The benchmarks here give some data data points about what statements are most efficient. In many programs, you will append or concat strings. The string.Format method is an alternative.