Introduction to string concatenation

Carl-Hugo Marcotte
5 min readMar 21, 2021

--

A beginner guide to programming with .NET 5 and C#

In this article, we dig a little more into the string type. We also explore how to concatenate (combine) strings. As a programmer, you will often need to manipulate strings, concatenation and interpolation being two recurring themes. We will cover interpolation in the next installment.

This article is part of a learn programming series where you need no prior knowledge of programming. If you want to learn how to program and want to learn it using .NET/C#, this is the right place. I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that’s not mandatory.

Strings

In C#, a string is a sequence of characters delimited by quotes ("). We used strings in previous articles to write text to the console.

As a reminder, here is how to create a string variable:

var name = "Superman";

We already used strings, so no need to linger too long here. Next, we explore some new content: multiline strings.

Multiline strings

In C#, we can create multiline strings by prefixing the string with the @ character. As the name implies, the only difference is the possibility to set the content of the string over multiple lines.

Here is an example of a multiline string:

var name = @"This
is
a
multiline
string!";
// More code here...

But keep an eye open for details. For example, in the following code, the first and last characters are a new line, which may not be what you expected to write.

var name = @"
This
is
a
multiline
string!
";
// More code here...

Multiline strings are handy in different scenarios where you don’t want to use concatenation. As an introduction to strings, I will not get into too many more details. However, there are many methods to manipulate strings, compare them, or optimize their usage.

Next, we look into the main subject: concatenation.

Concatenation

Concatenation is the action of piecing multiple strings together to make a new one; combining strings if you wish. In C#, the concatenation operator is the + symbol. The+ operator combines both string operands (the values on the left and right sides) into a new string. Let's look into an example to make learning easier:

var concatenatedString = "Greetings " + "from .NET!";
Console.WriteLine(concatenatedString);

The preceding code combines the strings "Greetings " and "from .NET!" into "Greetings from .NET!".

Note the space after the word Greetings. Without it, the combined string would have been "Greetingsfrom .NET!".

Another way to do it, which is more likely to happen than what we just did, is assigning a value to a variable and then updating that value. Here is an example of that:

var concatenatedString = "Greetings ";
concatenatedString = concatenatedString + "from .NET!";
Console.WriteLine(concatenatedString);

The preceding code does the same as "Greetings " + "from .NET!", but in two steps (bullets 1 and 2):

  1. The value "Greetings " is assigned to the concatenatedString variable.
  2. The value "from .NET!" is added at the end of the concatenatedString variable, then reassigned to it.
  3. The program writes "Greetings from .NET!" to the console.

Let’s look more into the line concatenatedString = concatenatedString + "from .NET!"; as it may be harder to understand at first.

Reminder: the assignation operator (=) has the lowest priority, so the code on the right (the right-hand operand) is processed first.

Here is an image to help analyze the code:

  1. The program ready the literal string "Greetings " for step 2.
  2. The program assigns the value of the right-hand operand ("Greetings ") to the concatenatedString variable.
  3. The program ready the current value of the concatenatedString variable for step 5 ("Greetings ").
  4. The program ready the literal string "from .NET!" for step 5.
  5. The program concatenates (combines) the two strings into a new one.
  6. The program assigns the value of the right-hand operand ("Greetings from .NET!") to the concatenatedString variable, replacing its value.

Note: You can see steps 3 to 5 as evaluated first, like one big step, then the program continues at step 6.

Now that we covered that, there is a shortcut to this process: the += operator. You can see the+= operator as a combination of both concatenation (+) and assignation (=). The previous example, using the+= operator, would look like this:

var concatenatedString = "Greetings ";
concatenatedString += "from .NET!";
Console.WriteLine(concatenatedString);

The second line of code is simplified compared to the previous bloc. It does the same, without the need to specify that concatenatedString = concatenatedString + [something else]. This new syntax removes unneeded pieces, reducing the length of the line of code, most likely even making it easier to read.

Enough theory; next, it’s your turn to try it out.

Exercise

To practice concatenation, we will update the code from the previous article’s exercise, but with a twist. Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post on my blog. I’m sorry for the inconvenience.

Conclusion

In this article, we learned that we could write multiline strings if we need to. We also learned about concatenation, which combines multiple strings into a new one. The concatenation operator is the symbol +. We also look at the+= operator. += allows us to combine both the concatenation and assignation operators. Using it can simplify our code and remove a step. In programs, concatenation is often used, which makes it an essential piece of knowledge to have.

Next step

It is now time to move to the next article: Introduction to string interpolation which is coming soon. Stay tuned by following me on Medium, Twitter, or other places you can find me. You can look at my blog contact page for more info.

Originally published at https://www.forevolve.com on March 21, 2021.

--

--

Carl-Hugo Marcotte
Carl-Hugo Marcotte

Written by Carl-Hugo Marcotte

Carl-Hugo is a software craftsman who has developed digital products professionally since 2005 with expertise in software architecture, C#, and .NET.