Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sometimes, we need characters in a string that are hard to represent in program code. Escape sequences can help.
Before we move on from strings, we should take a moment to look at something called escape sequences. We won't be using these in the cat food store, but you'll definitely need to use them in other programs.
- Sometimes, we need characters in a string that are hard to represent in program code.
- The first line of code is supposed to print a string that includes a newline character, but we get a "Newline in constant" error when we try to run the code.
- We want the "new paragraph" line to include a tab character to indent some text, but when I pressed the Tab key, my editor inserted space characters instead.
- And we're getting more errors from the line that includes a quotation. It's supposed to be a single string that contains double quote marks, but what we wind up with is a a string containing
"He said, "
, code referring to a nonexistent value namedWhoa
, and a second, empty string at the end.
static void Main(string[] args)
{
Console.WriteLine("first line
second line");
Console.WriteLine(" new paragraph");
Console.WriteLine("He said, "Whoa."");
}
Escape sequences let you include characters in strings that might otherwise be hard to represent in code.
Newline: \n
skips to a
new line
Tab: \t
Indents text
Double-quotes: \"
Inserts "double quotes"
Backslash: \\
Inserts a \ backslash
- Let's update the previous program to use escape sequences for the hard-to-represent characters.
static void Main(string[] args)
{
Console.WriteLine("first line\nsecond line");
Console.WriteLine("\tnew paragraph");
Console.WriteLine("He said, \"Whoa.\"");
Console.WriteLine("A backslash: \\");
}
-
0:00
Before we move on from strings,
-
0:02
we should take a moment to look at something called escape sequences.
-
0:06
We won't be using these in the cat food store.
-
0:08
But you'll definitely need to use them in other programs.
-
0:11
Sometimes we need characters in the string that are hard to represent
-
0:14
in program code.
-
0:16
This first line of code is supposed to pull a string that includes a new line
-
0:19
character.
-
0:20
But we get a new line and constant error when we to run the code.
-
0:25
We want the new paragraph line to include a Tab character to indent some text.
-
0:29
But when I press the Tab key, my editor inserted space characters instead, and
-
0:35
we're getting more errors from the line that includes a quotation.
-
0:38
It's supposed to be a single string that contains double quote marks.
-
0:42
But what we wind up with is a string containing He said, code referring
-
0:46
to a non existent value named Whoa, and a second empty string at the end.
-
0:51
Escape sequences allow you to include hard to represent characters within your
-
0:55
strings.
-
0:56
They usually consist of a backslash followed by another character.
-
1:00
To represent a newline character, you can type \n within a string.
-
1:05
To represent a tab, you can type \t.
-
1:08
To represent double quotes, you can type \" character.
-
1:14
And if you want to represent a backslash, type \\.
-
1:19
Let's update this program to use escape sequences for
-
1:22
the hard to represent characters.
-
1:24
I'll take the new line within this first line and
-
1:27
replace it with \n to represent a new line character.
-
1:31
I'll take these two spaces that are supposed to represent a tab and
-
1:34
replace it with \t so we get an actual tab character.
-
1:38
And I'll put backslashes before both of the extra quote characters so
-
1:42
that we can have quotation marks within our quoted string.
-
1:46
Finally, let's add one extra line that prints a backslash character.
-
1:50
We do that with a double backslash within our quoted string.
-
1:54
Now let's try running this and see if our syntax errors go away.
-
2:01
I'll hit the up arrow to bring up our dotnet run command and hit Enter, and
-
2:05
this time everything works.
-
2:08
Our first line of code results in the string first line being printed,
-
2:13
followed by a new line, followed by the text's second line.
-
2:16
Our second line of code results in a Tab character being printed,
-
2:19
followed by our text.
-
2:21
We have our quotation marks included in the output by our third line of code.
-
2:27
And in our fourth line of code,
-
2:28
the double backslash within the string gets replaced with a single backslash.
-
2:32
You've learned all the basics of working with strings in C#.
-
2:35
You can append strings to other strings by concatenating them.
-
2:39
You can embed data within strings using interpolation.
-
2:42
And you can embed special characters using Escape sequences.
-
2:46
Up next, our cat food store needs to take the number of cans the user entered and
-
2:50
calculate the total cost.
-
2:52
That's going to require working with numbers in math.
-
2:55
We'll look at both of those topics in the next stage.
You need to sign up for Treehouse in order to download course files.
Sign up