In this quick tutorial, we are going to talk about different ways to concatenate strings. Splitting and joining strings are commonly used operations in almost any real-world application, so let’s see how we can concatenate strings through some examples.

We won’t cover methods that are not specifically meant for concatenating strings, like Enumerable.Aggregate() or any other generic methods that reuse the mechanisms from this article.

Also, this is not an article about the performance of these methods, so be careful when using them.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
The source code is available on the GitHub repo.

Let’s dive in.

What is String Concatenation?

Strings in C# are immutable which means that a string cannot be changed once it has been created. Any method or operator that appears to be changing the string is actually creating an entirely new string object. This is important to understand before we proceed further.

As for the term “concatenate”, it’s a fancy way of saying “join” and it’s used to describe the merging of two or more string literals or objects. It’s a very common operation in C# as well as any other programming language.

Besides concatenation, the String class provides us with many other useful methods. You can learn more about it in our C# – Back To Basics article about strings.

Using the + and the += Operators

These operators are familiar to anyone doing software programming, even those just beginning. They are easy to use and intuitive since we all did math in school. The + operator is not overloaded by the String class and if you’re concatenating string variables it’s translated to String.Concat() in the IL.

Let’s take a look at the simplest example:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";

string foobar = foo + " " + bar;
Console.WriteLine(foobar); // outputs "Morning! Nice day for fishing, ain't it?"

This is equal to System.String.Concat(foo, " ", bar).

On the other hand, if you’re using just the string literals:

string foobar = "Morning!" + " " + "Nice day for fishing, ain't it?";
Console.WriteLine(foobar); // outputs "Morning! Nice day for fishing, ain't it?"

This won’t trigger the Concat method to be called and the value will be pushed to stack directly. Constants and literals concatenation happens at compile-time, and for the variables at runtime.

The += operator works similarly:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";
string[] foobar = { foo, " ", bar };

string plusEqualsResult = String.Empty;

for (var index = 0; index < foobar.Length; index++)
{
	plusEqualsResult += foobar[index];
}

Console.WriteLine(plusEqualsResult); // outputs "Morning! Nice day for fishing, ain't it?"

That’s it, pretty simple and intuitive.

String.Format() Method

The String.Format() method is a method of the String class that helps us join and format our strings in various ways. It has eight overloads, and it’s very versatile.

The most commonly used looks like this: static String Format(String format, params object?[] args);

Basically, that means it can insert any number of different objects into the string we provide, by using a special way to do so. Namely, you can insert one or more symbols {n} into the provided, where n starts at index 0. Once you’ve done that, you provide as many arguments as you have inserted placeholders.

This might sound a bit confusing, so let’s see how it works in a simple example:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";

string foobar = String.Format("{0} {1}", foo, bar);
Console.WriteLine(foobar); // outputs "Morning! Nice day for fishing, ain't it?"

As you can see, {0} and {1} represent foo and bar respectively. If there are more “inserts” than provided arguments, we’ll get a FormatException from the compiler.

So this is invalid:

string foobar = String.Format("{0} {1} {2}", foo, bar);

There are 3 places to insert strings, and only two arguments are provided, so this would cause the FormatException to be thrown.

Nowadays, since C# 6 came out, we have a decent replacement for String.Format(), and it’s called string interpolation. We’ll see how string interpolation works a bit further on.

String.Concat() Method

The Concat method is the bread and butter of string concatenation. It’s used both explicitly and by the compiler to concatenate strings. It really powerful and versatile, and it has 13 different overloads.

You can concatenate two or more strings, strings from an array, or IEnumerable and much more. We’ve even mentioned that + and += operators are translated to String.Concat() by the compiler.

When in doubt you can use String.Concat() to join strings:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";

string foobar = String.Concat(foo, " ", bar); // outputs "Morning! Nice day for fishing, ain't it?"

Pretty straightforward and easy to use.

Using String Interpolation (C# 6 Feature)

String interpolation is the newest way of joining multiple strings. It’s very intuitive and easy to use. Once you learn to use interpolation you might even ditch String.Format() for good. String.Format() still has some usages though, but for the most part, you can use interpolation instead.

To start using string interpolation to format your strings, you need to prefix a string with a ‘$’ character. After that, you can use braces { and } to insert variables in such a string. Since braces are a special character, you can use double braces {{ and }} to write a brace in the interpolated string.

You can also create an interpolated verbatim string by using ‘$’ and then ‘@’ (or in reverse order) to prefix the string.

Let’s see how interpolation works in practice:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";

string foobar = $"{foo} {bar}"; // outputs "Morning! Nice day for fishing, ain't it?"

As you can see it’s by far the least cumbersome and simple way of concatenating strings while formatting them at the same time.

String interpolation is actually much more powerful and it can format strings in various ways.

Joining Strings with String.Join() Method

Another way of joining strings is by using a String.Join() method.

But what makes String.Join() different from other methods we used so far?

String.Join() specializes in concatenating elements of an array or collection of strings or objects, and we decide how by providing a separator to the method.

So if we have an array or a list of strings, and we need to join them, by using some kind of separator (can be an empty space too), we would use the String.Join() method.

Let’s see how that works in our example:

string foo = "Morning!";
string bar = "Nice day for fishing, ain't it?";
string foobar = { foo, bar };

string foobar = String.Join(" ", foobar); // outputs "Morning! Nice day for fishing, ain't it?"

This example doesn’t do it justice, but say for example you have an array of integers and you want to join them into one string and separate them by a comma:

int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
string foobar = String.Join(",", array); // outputs "1,2,3,4,5,6,7,8,9"

Okay, let’s move on to the last method.

StringBuilder.Append() Method

StringBuilder is a class that exists solely for the purpose of overcoming the string immutability “problem”. When working with a lot of string objects, we can easily allocate much more memory than we actually need to since every operation on those strings requires a new memory piece to be allocated.

So to avoid this problem, the StringBuilder class has been created, and it doesn’t create new strings when it modifies them.

So why don’t we use StringBuilder everywhere you might ask?

Well, StringBuilder is an overhead if you need to concatenate just a few strings. It doesn’t pay off. But a few hundred or more, that’s when you should be thinking about using StringBuilder class.

Let’s see how we can concatenate or rather append strings with the StringBuilder.Append() method:

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.Append(foo);
stringBuilder.Append(" ");
stringBuilder.Append(bar);

Console.WriteLine(stringBuilder) // outputs "Morning! Nice day for fishing, ain't it?"

Again, this example is just to demonstrate how StringBuilder works. This is not the real case to use it. And you can see a bit overhead there, especially comparing it to something like string interpolation.

Conclusion

In this short tutorial, we’ve gone through the different ways of concatenating strings in C#. Mind you that these are not the only ways to do it since C# is pretty flexible, but these are the string-specific ways that were meant to be used for this purpose.

Now that you know how to concatenate strings in multiple ways, go ahead and play around a bit. There are some pretty fun things you can pull off. Nice day for fishing, ain’t it?

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!