TOC

The community is working on translating this tutorial into Afar, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Data types:

Strings

A string is a piece of text. It's usually consists of 2 characters or more, since if it's just one, you should consider using a char instead. However, strings can be empty as well or even null, since it's a reference type. A string can be declared much like the other data types we have worked with already:

string s;

Or if you want to assign a value to it immediately:

string name = "John Doe";

Anything within a set of double-quotes in C# is considered a string, like in the example above - surrounding one or several characters between double quotes is simply how you tell the compiler that the stuff between it should be interpreted as a string instead of e.g. keywords and commands.

Strings are immutable

In C#, strings are immutable, which basically means that once they are created, they can't be changed. That's obviously not very practical in daily use, so the framework helps us - instead of having to keep declaring new strings to make changes, it simply creates a new string for you each time you change the existing one. This makes it a seamless process, but it can also make your code less efficient without you even noticing it. Here's an example to illustrate it:

string numbers = "";
for (int i = 0; i < 10000; i++)
    numbers += i.ToString();

In this case, we loop 10.000 times, each time appending the current index to the string. With the knowledge you just gained, you now know that instead of altering the existing string to include the latest number, a new string is created and then assigned to the old variable, leaving the old value to be cleaned up by the framework. And this happens 10.000 times! Instead, it's generally recommended to use a so-called StringBuilder if you know that you will be building a string over several operations:

StringBuilder numbers = new StringBuilder();
for (int i = 0; i < 10000; i++)
    numbers.Append(i);
Console.WriteLine(numbers.ToString());

Basic string operations

With all that about the strings being immutable said, you will still find your self manipulating and working with simple strings a lot, and don't worry about it - unless you are working inside a giant loop, it's likely no problem! Here are some of the basic stuff you can do with strings:

You can concatenate two or more strings simply by "adding" them (using the plus operator):

string name = "John" + " " + "Doe";

You can of course do the same with variables, where you mix double-quoted strings and variables which are either strings or which can be turned into strings (using the ToString() method found on all objects). However, a more "clean" way of doing that is with the Format method found on the String class:

string name = "John Doe";
int age = 42;
string userString = String.Format("{0} is {1} years old and lives in {2}", name, age, "New York");

Notice how I use numbered placeholders ({0}, {1} etc.) and then supply the values for it as parameters to the method. Just remember that the indexes and amount of placeholders has to match the parameters you pass!

The Length property allows you to check the current length of a string, e.g. for validation purposes. The Length property is also very useful in combination with other properties and methods, e.g. the Substring() and the IndexOf() methods. The Substring method allows you to retrieve a part of the string, while the IndexOf method allows you to find the first index of a given char/string. Allow me to illustrate with an example:

string name = "John Doe";
int indexOfSpace = name.IndexOf(' ') + 1;
string lastName = name.Substring(indexOfSpace, name.Length - indexOfSpace);
Console.WriteLine(lastName);

Quick explanation: We define a name, then we use the IndexOf() method to find the first position of a space character. Then we use the Substring() method to get everything after the space character by supplying a start position as well as a length.

Another cool helper method on the String class is the Replace() method. It allows you to take a string and then run a search/replace operation on it, like this:

string name = "John Doe";
Console.WriteLine(name.Replace("John", "Jane"));

The Replace() method is not strict at all - if the string you're searching for (the first parameter) is not present, then nothing will happen (no exceptions are thrown or anything like that). If it is present, it will be replaced with the second parameter. However, if you want to check before replacing, you can use the Contains() method:

string name = "John Doe";
if (name.Contains("John"))
    Console.WriteLine(name.Replace("John", "Jane"));
else
    Console.WriteLine("John was not found!");

Sometimes you want to know if a string starts or ends with a specific char or string. For that, the String class can help you with the StartsWith() and EndsWith() methods, which works just as the name indicates:

string name = "John Doe";
if ((name.StartsWith("John")) && (name.EndsWith("Doe")))
    Console.WriteLine("Hello, Mr. Doe!");

There are even more cool String methods, and there are definitely more ways of using them than illustrated by these short examples. If you want to know more about it, have a look at the MSDN documentation for the String class.

Verbatim Strings & Escaping

When defining a string, you will soon see that certain characters have special purposes. The most important example of this is the double quote it self, because since it's used to mark the start and end of a string to the compiler, how can you use it inside your string? The most simple answer to this is escaping, where you signal to the compiler that a special character should be treated literally instead of its usual function. Here's an example:

Console.WriteLine("What do you mean by \"Hello, world\" ??");

When executed, the result will look like this:

What do you mean by "Hello, world" ??

In other words, we simply use backslashes before a double quote, to indicate that this is NOT the end of the string, but instead, we actually want a double quote. So... what if we actually want a backslash and not just use it to escape another character? Well then you will have to escape that too - with another backslash:

Console.WriteLine("Right here \\ Right now");

Resulting in:

Right here \ Right now

The reason is that the backslash is not just used to escape double quotes - it's also used to prefix a range of characters to give them special meanings. For instance, \n is a new line, \t is a tab character and so on. You can find a nice list of escape sequences here.

Verbatim strings

As an alternative to all this escaping, you can use a verbatim string. It's like a regular string declaration, but prefixed with the @ character, and inside it, all characters are treated literally:

Console.WriteLine(@"In a verbatim string \ everything is literal: \n & \t");

The output will look just like the input:

In a verbatim string \ everything is literal: \n & \t

There's just one exception to this rule: A double quote still has to be escaped, but that makes pretty good sense, because otherwise, how would the compiler know if you're trying to end the string or not? In a verbatim string however, a double quote is not escaped with a backslash but instead with another double quote, like this:

Console.WriteLine(@"What do you mean by ""Hello, world"" ??");

The result looks like this:

What do you mean by "Hello, world" ??

Summary

Strings are SUCH an important topic for all programmers - you will likely be doing some sort of string processing for much of the time you spent writing code. Fortunately, C# has all the tools you will need to do it, as I have tried to illustrate in this article.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!