As a programmer you’ll always have to deal with the strings. In this article you’ll learn the most basic string operations: concatenating/joining and splitting.
Concatenation means that you append a one string at the end of another string. This happens directly or indirectly, but you should know that there are a few ways you can join strings in C#.
The + operator
The simplest method of joining strings together is the + operator. It’s as easy as putting + in between strings:
Note, that NULLs are treated the same way as empty strings.
String are immutable
When we say something is immutable we mean that its state cannot be changed after it has been created. In practice it means that we cannot alert string. Any string based operation creates new string which we must reassing to a variable. I’ll give you an example, the code below uses Substring method which takes three first characters from a string, but it won’t alter the string value:
Concatenting string with other types
Since we can use + to join one or more strings you may be wondering what would happen if we try to concatenate a string with a type other that string like int. This feature is already included in C#:
How does this work? The solution is very simple, in the end it calls ToString method on all the non string variables (so it’s more like
This is pretty straightforward method, all it does is concatenate strings together. It also has an overload which we can use to pass an IEnumerable.
The drawback of this method is that you cannot use a delimiter (separator) of any kind, so the strings are joined back to back.
The .Join() method
Imagine that you have a long list of strings, certainly you wouldn’t want to use + to manually concatenate them. Neither you would want to write a loop to join these strings every time.
So how can we combine a list (or an array, or anything implementing IEnumerable for that matter)? Fortunately for us there’s a handy string.Join method, which we can use to make a short one-liner:
Using string.Join has more advantages. It allows us to put any separator (the value in between each pair of strings) and the separator won’t be repeated at the end.
While this is another way of joining strings it allows additional formatting e.g. for numbers. The first parameter is the output string before other values are put into it, then the following parameters are the values.
At times it’s more readable to use string interpolation. It may not exacly be the same as String.Format, but you can see it as an alternative syntax for String.Format since it’s very similar.
There will come a time when you’ll want to combine large amount of strings. For this kind of scenario it’s best to use StringBuilder class.
When we split a string we expect to get an array of substrings. For this we have a string.Split method.
Lets start off with a (simplified) CSV example. CSV is a text file which has rows (each row is a new line) and columns (each column is separated by delimiter e.g. semicolon). To get the values of each column we need to split each line by a delimiter like this:
In the previous example we used a single character delimiter, but we can also pass an array of characters, a single string or an array of strings.
It’s also worth noting that you can split by whitespace characters like space, tab or new line.
There’re also two little tricks you can use with string.Split method. First one is that there’s an overload which takes StringSplitOptions as second parameter. We can use it to make sure that there will be no empty entries:
Second trick is a little bit less known. You can pass null to the string.Split method. It will split your input string by whitespace characters.