Locked lesson.
About this lesson
We learn the difference between Concatenation and Interpolation, and when one might be easier to use than the other.
Exercise files
Download this lesson’s related exercise files.
12 - String Concatenation and Interpolation.docx60.8 KB 12 - String Concatenation and Interpolation SOLUTION.docx
57.7 KB
Quick reference
String Concatenation and Interpolation
String concatenation and interpolation allow you to add things to your strings, or edit them in real-time.
When to use
Use these whenever you need to modify your string.
Instructions
string name = "John";
Console.WriteLine(name + " Elder"); // concatenation
Console.WriteLine($"{name} Elder");
Hints & tips
- Concatenation uses "+" signs to add pieces of text together: Console.WriteLine(name + " Elder");
- Interpolation references variables within the WriteLine statement: Console.WriteLine($"{name} Elder");
- 00:04 Okay, in this video, I want to talk about string concatenation and interpolation.
- 00:07 And we've already talked about this a little bit.
- 00:09 We've been using the + sign to concatenate things, but
- 00:11 there's different ways you can do it.
- 00:13 And there's a couple of other things I want to show you about that, and
- 00:16 it's kind of important.
- 00:17 So, let's create a string, and call this,
- 00:21 firstName, and set that = John.
- 00:27 And let's create a string with lastName, and let's set that = Elder.
- 00:32 And we've concatenated these things before, so, we could go Hello, and
- 00:38 then use the + sign, and then say firstName, and then another + sign, and
- 00:44 then put a space in between, and another + sign, and then say lastName.
- 00:49 So, if we run this, we could pretty well figure out what this is going to be.
- 00:55 We see it says, Hello John Elder.
- 00:56 And that's fine, you can do that way, that's perfectly normal.
- 00:59 Well, there's a second way you can concatenate strings.
- 01:03 And that is just to use a little placeholder like this, and
- 01:07 stick a 0 in there.
- 01:08 Now, we can put a comma at the end of this and say, firstName, for instance, right?
- 01:15 So, this will sort of substitute whatever's in here with whatever's
- 01:19 out here.
- 01:20 So, if we save this and run it, We get what we would expect, Hello John.
- 01:27 So, that's sort of interesting, and that's sort of useful.
- 01:30 But the real fun stuff here is interpolation.
- 01:33 We can use this placeholder, but instead of sticking a 0 here,
- 01:37 we can just use a variable.
- 01:40 Now, you'll notice, when I do this, nothing changes,
- 01:43 the color doesn't change here.
- 01:44 And if we run this, we're going to get an error.
- 01:48 And sure enough, well, not so much an error, it just prints out firstName.
- 01:53 And that's, definitely, not what we want.
- 01:55 So to make this interpolation work, string interpolation, which is what this is,
- 01:59 we need to stick a $ sign at the beginning of our string.
- 02:02 And you can see, now, this has changed colors, it looks a little different,
- 02:06 it looks like a variable should.
- 02:08 So, now, when we run this, we see it says, Hello John.
- 02:12 And I find this a much easier way to concatenate strings.
- 02:16 You don't have to use messy + signs all the time.
- 02:19 Plus you can do stuff in this string, still.
- 02:21 So, if we wanted to put firstName and lastName,
- 02:25 you might be tempted to go like this, lastName.
- 02:28 But you'll notice this doesn't change color.
- 02:31 And if we run this we're going to get an error, there's problems.
- 02:34 So, when you want to use more than one variable like this,
- 02:37 you have to use more than one set of brackets, which is fine.
- 02:40 This just kind of makes more sense, anyway.
- 02:43 So, here we can go, lastName, and when we do that, make sure we spell it right,
- 02:48 boom, it changes colors like a variable should.
- 02:50 And you'll notice there's a space in here, we didn't have to do any ugly +, and
- 02:55 then quotation marks with a space in it, and then another set of +.
- 02:59 We could just do it like this, works perfectly good.
- 03:01 Make sure we have our $ sign up here.
- 03:03 We're on this guy.
- 03:06 We see Hello John Elder, which is exactly what we would expect.
- 03:09 And it's pretty cool.
- 03:10 So, that's a couple of different ways to do use string concatenation and
- 03:14 string interpolation.
- 03:16 Personally, I like to use interpolation whenever I have strings that need to be
- 03:20 used with variables, variables that need to be used with strings.
- 03:23 Either of these three different ways works.
- 03:26 And as you code more often you'll find, well, this is better for
- 03:29 this circumstance, and that's better for that circumstance.
- 03:32 And we're not going to go into all that in this video, you'll sort of pick that up on
- 03:36 your own, over time, as you develop your own coding style.
- 03:39 There's no right or wrong way to do it, usually.
- 03:42 Sometimes there is, but usually it's just personal preference.
- 03:45 For me, I like the interpolation, super easy, and it's all there is to it.
- 03:49 So, in the next video we're going to start looking at math.
Lesson notes are only available for subscribers.