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.

Concatenating/joining strings

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:

1
2
3
4
5
string welcome = "Hello" + "World";

string firstName = "John";
string lastName = "Doe";
string fullName = firstname + " " + lastName;

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:

1
2
3
4
5
6
7
8
string title = "developer";
// this will print: dev
Console.WriteLine(title.Substring(3));
// while this will still print: developer
// because Substring operation created a new string
// if we reassing it back
title = title.Substring(3);
// then title == "dev"

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#:

1
2
string something = "Hello" + 123;
// something == "Hello123"

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 123.ToString()).

String.Concat

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.

1
2
var names = new [] { "John", "Jane" };
var result = String.Concat(names);

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
string users = names[0] + "," + names[1] + "," /* ... */;

string names = string.Empty;
for(string user in users)
{
    names += user + ", ";
}

// as a another drawback the for loop left us with
// a string which has ", " and the end

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:

1
2
// second parameter is a separator
string users = string.Join(names, ", ");

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.

String.Format

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.

1
2
3
4
// {0} - first parameter, without additional formatting
// {1:0.00} - second parameter, format with numbers after decimal point
// {2:d} - third parameter, format with "d", which formats date to be printed without time part
var result = String.Format("Hello {0}. The lucky number for {1:0.00} is {2:d}", "person", 3.14, DateTime.Now);

String interpolation

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.

1
2
string name = "Zbigniew";
string welcome = $"Hello {name}";

StringBuilder

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.

1
2
3
4
5
6
var stringBuilder = new StringBuilder();
foreach(var row in rows)
{
    stringBuilder.AppendLine($"{row.FirstName} {row.LastName} is from {row.Country}");
}
var result = stringBuilder.ToString();

String splitting

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:

1
2
var row = "1;Jane;Doe;Software developer;C#";
var columns = row.Split(';');

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.

1
2
3
4
var input "Some input some string";
var splitByChars = input.Split(new [] { 'i', 's' });
var splitByString = input.Split(" ");
var splitByStrings = input.Split(new [] { "some", "i" });

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:

1
var result = someString.Split(" ", StringSplitOptions.RemoveEmptyEntries);

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.

1
var result = "aa bb \tcc\r\n".Split(null);