Concatenate multiple strings in C#
This article illustrates the different techniques to concatenate multiple strings in C#.
1. Using String.Concat() method
A simple solution to concatenate multiple strings together is using the String.Concat() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = String.Concat(s1, s2, s3); Console.WriteLine(s); // C# 8 } } |
2. Using String Concatenation Operator
Alternatively, you can also use the traditional string concatenation operator + to concatenate string literals in C#. The compiler internally translates this call into the String.Concat() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = s1 + s2 + s3; Console.WriteLine(s); // C# 8 } } |
3. Using StringBuilder class
The most efficient solution to concatenate several string values together is using StringBuilder. You can use its Append() method, which is overloaded for all primitive types and generally performs better than the String.Concat() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Text; public class Example { public static string Concatenate(params string[] strings) { StringBuilder sb = new StringBuilder(); foreach (string s in strings) { sb.Append(s); } return sb.ToString(); } public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = Concatenate(s1, s2, s3); Console.WriteLine(s); // C# 8 } } |
Quoting Microsoft Docs, “Performance depends on the size of the string, the amount of memory to be allocated for the new string, the system on which your code is executing, and the type of operation. You should be prepared to test your code to determine whether StringBuilder actually offers a significant performance improvement.”
4. Using String Interpolation
Starting with C# 6, one can use String interpolation ($), which provides a convenient syntax for concatenating multiple values together to form a String. The following code example uses string interpolation ($) to concatenate three strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Text; public class Example { public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = $"{s1}{s2}{s3}"; Console.WriteLine(s); // C# 8 } } |
5. Using String.Format() method
Before C# 6, you can use the String.Format() method in place of string interpolation to concatenate strings in C#, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = String.Format("{0}{1}{2}", s1, s2, s3); Console.WriteLine(s); // C# 8 } } |
6. Using String.Join() method
Another option is to construct a string array from the given strings and pass it to the String.Join() method with an empty string as a delimiter. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; public class Example { public static void Main() { string s1 = "C#"; string s2 = " "; string s3 = "8"; string s = String.Join(String.Empty, new string[] {s1, s2, s3}); Console.WriteLine(s); // C# 8 } } |
That’s all about concatenating multiple strings in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)