Understanding the String Class

A C# string is a read-only sequence of characters; read them by index, write special characters with escape sequences, and understand why strings never change.

Novice · C# · Updated

After this lesson you can

  • Read a string's characters by index and find its Length
  • Write special characters in a string with escape sequences

Key takeaways

  • A string is characters in order: team[0] is the first char, and Length says how many there are, so the last is at Length - 1.
  • Strings never change. Methods such as ToUpper return a new string, so store it if you want to keep it: name = name.ToUpper();.
  • Write special characters with escape sequences: \n for a new line, \t for a tab, \" for a double quote and \\ for a backslash.
  • An @ in front of a string makes it verbatim, so the backslashes in @"C:\bootcamp\strings" are just backslashes.
  • The empty string "" has a Length of 0; string.IsNullOrEmpty checks for it and for null, which means no string at all.

Text is everywhere in programs: names, addresses, messages, web pages, file contents. In C#, text is a string, and you've used strings since your first program. This chapter looks inside them.

A string is a sequence of characters

A string is an ordered sequence of char values, one per character. Like an array, each character has an index starting at 0, and Length says how many there are:

string team = "Georgia";

Console.WriteLine(team.Length);
Console.WriteLine(team[0]);
Console.WriteLine(team[team.Length - 1]);
7
G
a

team[0] is a char, the single character 'G'. You can loop through a string's characters just as you loop through an array:

string word = "loop";

foreach (char letter in word)
{
    Console.Write(letter + "-");
}
Console.WriteLine();
l-o-o-p-

Strings never change

Strings are immutable: once a string exists, its characters can't be changed. You can't assign to team[0]. Every operation that seems to change a string actually makes a new string:

string name = "Vanessa";
string fullName = name + " Jones";
string shouting = name.ToUpper();

Console.WriteLine(name);
Console.WriteLine(fullName);
Console.WriteLine(shouting);
Vanessa
Vanessa Jones
VANESSA

name is still "Vanessa". To keep a changed version, store the new string, even in the same variable: name = name.ToUpper();.

Special characters: escape sequences

Some characters can't be typed straight into a string, such as a double quote (it would end the string) or a new line. You write them as an escape sequence: a backslash followed by a letter or symbol.

Escape Means
\n a new line
\t a tab
\" a double quote
\\ a backslash
Console.WriteLine("Line one\nLine two");
Console.WriteLine("She said \"hello\".");
Console.WriteLine("Name:\tGeorgia");
Console.WriteLine("C:\\bootcamp\\strings");
Line one
Line two
She said "hello".
Name:	Georgia
C:\bootcamp\strings

Strings with backslashes: verbatim strings

Windows file paths are full of backslashes. Put @ in front of a string to make it verbatim: backslashes are just backslashes, and nothing is escaped.

string folder = @"C:\bootcamp\strings";
Console.WriteLine(folder);
C:\bootcamp\strings

Empty strings

"" is the empty string: a string with no characters, and a Length of 0. string.IsNullOrEmpty checks for it, and also for null, which means no string at all:

string middleName = "";

Console.WriteLine(middleName.Length);
Console.WriteLine(string.IsNullOrEmpty(middleName));
0
True

Check yourself

2 questions to try on the lesson page.

So what

A string is characters in order, numbered from 0, and it never changes in place. Every string method you'll meet next follows from those two facts.

Key terms

Cheat sheets