6 Different Ways to Join Strings in C#
C# .NET How To

6 Different Ways to Join Strings in C#

Mishel Shaji
Mishel Shaji

In this article, I'll explain 6 different ways to join strings in C#. We'll also benchmark these methods to find the fastest and the most efficient way to join strings in C#.

1) String Concatenation

This is the most commonly used method to join two or more strings in almost all programming languages. In C# also, we can use the + operator to join two or more strings.

var firstName = "John";
var lastName = "Doe";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);

The problem with this method is that it makes the code less readable and difficult to understand when we use different variables and data types together.

int i = 5;
int j = 6;
string result = i + " + " + j + " = " + (i + j);
Console.WriteLine(result);

This code is difficult to read and understand. See how the next method solves this problem.

2) String Interpolation

This is my favorite way to join strings in C#. To use this method, we should add a $ symbol before the string value and wrap variables and expressions in {} brackets.

int i = 5;
int j = 6;
string result = $"{i} + {j} = {i + j}";
Console.WriteLine(result);

We are not limited to using simple variables here, we can also write expressions in the brackets.

var firstName = "John";
var lastName = "Doe";
string fullName = $"{firstName.ToUpper()} {lastName.ToUpper()}";
Console.WriteLine(fullName);

Console.WriteLine($"The result is { 5*2 }");

3) String.Join Method

With the string.Join() method, we can join multiple string values separated by a given separator.

var firstName = "John";
var lastName = "Doe";
string fullName = string.Join(" ", firstName, lastName);
Console.WriteLine(fullName);

The first parameter this method accepts is a separator. The second parameter is a params array.

4) String.Concat Method

Another way to join strings in C# is to use the Concat() method. We can pass multiple strings or an IEnumerable<string> type to the method as parameters.

var firstName = "John";
var lastName = "Doe";
string fullName = string.Concat(firstName, " ", lastName);
Console.WriteLine(fullName);

Or use the following method:

var strings = new string[] { "One", "Two", "Three" };
Console.WriteLine(string.Concat(strings));

5) String Format Method

String formatting is also one of the most commonly used methods in C# to join strings. Here's an example.

var firstName = "John";
var lastName = "Doe";
int age = 25;
var message = string.Format("{0} {1} is {2} year old.", firstName, lastName, age);
Console.WriteLine(message);

We can also pass an array of objects as the parameters. This is different from the Concat() method that can accept only IEnumerable<string> as the second parameter.

6) String Builder

String Builder can be used if you want to create and work with large strings. For simple string join operations, use any of the above-mentioned methods.

var firstName = "John";
var lastName = "Doe";
var builder = new StringBuilder();
builder.Append("My Name Is");
builder.Append(firstName);
builder.Append(" ");
builder.Append(lastName);
builder.AppendLine("----------");
builder.AppendFormat("I am {0} year old", 25);
Console.WriteLine(builder.ToString());

These are the six different ways to join strings in C#. If you have any questions, let me know in the comments below.