Hey friends, we're learning C sharp today and we're doing more with strings. Now, string, even that word already is weird and confusing. Yeah.
We kind of threw it in there and we said, hello world or hello David. And we said, that's a string in quotes. What's a string? Where is the string? This looks like text.
Yeah, exactly. So, strings are a sequence of characters strung together into a string of characters. So, each letter in that Maria.
M is a character but Maria together is a string. And in C sharp, strings are described in quotes. So, those two quotes that surround Maria and hello world, that denotes the string.
Okay. So, it's a string of characters. So, a character is like a thing, like an A or a B or a C. Right.
An atom. An atom. If you must. Okay. And then we string it together.
Right. It's a molecule, right? Yeah.
Or a friendship bracelet. That's right. Where I've got those little square letters.
The beads, right? Little beads and I put them onto the thing. So here, hello David, now does the quote, is that a string too or is that part of it?
You said it denotes it. That denotes the literal string. So those beginning and ending quotes mean this sequence of characters in between those quotes are the string contents, the actual data for a string.
Now I notice here that we also said, I'm assuming that this means here's a string called first friend that equals Maria and then that feels like the end of something. Semicolon, right? Right. End of a sentence, right?
Yeah. So string, you're trying to declare space, right? A variable called first friend and in that slot, you're going to store the string Maria, right?
The semicolon is a C-sharp way of denoting end of a statement. So we declared string first friend is Maria and semicolon is like a period, right? first sentence. That's your full stop.
Okay. This reminds me a little bit of algebra. Even though we're not doing anything with numbers here, we're going to do numbers later. It's like saying X equals one. Right.
Exactly. Same concept, just different type of data. And when you're doing math and you're learning algebra in school, you're just like, X, we'll make X. It's three.
So you just made that up. It doesn't matter that it's called first friend. You could have called it F.
Could be any name. Any name at all. Then though, you said, hey.
Console.WriteLine again. My friends are... But then what's going on here? What's this dollar sign?
Aha! This is a feature called string interpolation. Interpolation.
Interpolation. That's a big fancy word. Way too complicated for a video. Yeah, it is very complicated. But put simply, it's a way to substitute literal string text.
So, my friends are is part of the string. and then I want to print the values of first friend and second friend within that string. Mm-hmm.
So you could think of it like string concatenation or taking a bunch of strings and putting them all together. So I took my friend is, is my first string. Mm-hmm. Plus, like you just did there, my second, my first friend, plus the and text, plus my second friend. So those two are almost equivalent, and we'll talk about details about why they're different later on.
Mm-hmm. But for the most part, string interpolation is a fancy way of saying I want to concatenate the values of first friend and second friend into a bigger string. Right.
So, I took those two strings that are atoms and I made a bigger string from those two strings. Gotcha. That seems like a lot.
Let's just break it down as we get towards the end of this because that's a lot of words. We heard concatenation to take two things and put them next to each other. Concatenate and then interpolate which is like interpret.
Yeah. Right? So here we just said my friend is, that's a string, plus Maria, plus and, and then our second friend Scott.
That's kind of tedious. I can look at that and I get it. Like line eight makes sense to me, even if I'm just learning a language. But I could imagine myself getting confused and maybe missing a quote. And I'm looking at that like now there's a squiggly and I'm stressing out.
What is that squiggly? Well, this squiggly is telling me, hang on, and is not a thing, syntax error. Suddenly, I've broken everything. I'm freaking out.
That is it telling me I made a mistake. It's like spellcheck. So C Sharp is one of its strengths is that it's a strongly typed programming language. Unlike JavaScript, for example, that would have been a runtime error, right? So that error would have happened on.NET Run with a JavaScript-like language.
But in C Sharp. the compiler tells you at IDE time, at compile time, you get these squiggles red for errors, yellow for warnings, and it tries to tell you about errors before you compile, before you run. That's one of the big advantages of having a language like C-sharp.
It's like doing spell check before you turn your homework in. Exactly. I'd much rather know from the computer.
I don't want my teacher to tell me that I made a mistake. You got it. So this is great.
So putting this dollar sign here, allows me to then refer to my variables, x, y, first friend, second friend. Right. Within those curly braces. And that's very friendly to read.
So line 6 makes a lot of sense. Both line 6 and 8 work, but when working with strings, I can totally do line 6. That's the modern and the new way to do it. That's correct. Very cool. This is new modern C sharp.
I like it. Alright. Other things that I can do with strings is I can trim them. Right.
So you can trim off some... spaces off the start or the end or both from the start and the end. Let's do that real quick as we get towards the end here.
Let's add some, actually, let's do this. Let's put a bunch of spaces inside of Maria. So, one useful thing, the spaces outside of quotes don't matter.
So, you could have string first friend equals space, space, space, quote Maria. Those first string, that, this space doesn't matter. Really?
Yeah. Can I do this? That's right.
Like that would be weird, but I can do that, right? Some people like weird. Okay. So I can do that and it doesn't end until it ends.
Correct. Okay, cool. But we're not going to do that. But this is a reminder, and you use the word denotes, that the string is inside of the quotes, of those two quotes. So Maria has some leading space.
That's right. And some trailing space. So over here, Is it going to include those? Let's find out. We're going to go to our terminal.
I'm using a hotkey. I'm saying control and I'm pushing the tilde button there. I'm going to type in.NET run. And let's see. Look, my friends are a lot of space and a lot of space.
If I want to trim that, we can trim that. We can do it a couple ways. I can think of two ways to do this. I could say first friend, and notice how I start typing and it's trying to help me. Ah, this is the other big plus of C-sharp.
Intellisense. Okay. So, you get completion of variables, of functions, of pretty much everything.
Yeah. And you see this in Google Docs and in Microsoft Office when you start typing and they go and you finish each other's sandwiches. Let me help you.
Exactly. So, I could say first friend equals, I could say something else, or I could say And then strings have all of these methods on them. So I'm going to trim. Let's just trim the start of Maria.
And I'll put a semicolon there for the end. So you replaced the value that's currently stored in first friend with the same string, but trimming off the starting white space. Exactly. So I'm expecting to see something like that.
Right. Let's prove that. We can go again.
I can go back to my command line if I want to. and type.NET run here or I could do that inside of Visual Studio Code or I could hit Control F5. Here we go.
My friends are and then that space is gone. So let's go back and let's just change that to trim completely and we'll say.NET run. There we go.
Nice. Then let's go ahead and do the same thing for Scott. Now here's the other way that I could do it. I could say second friend. dot trim.
Right there in line. So right there, and you said in line. Right.
That's a great word. Directly. In this line, all in one line. This is our variable second friend.
We can say dot to call a method. We're going to talk about that when we get to object-oriented programming. And we're going to say trim. And just like the functions that you saw in math, where you say like f of x, we're going to say trim.
And then the parentheses are there to tell you that I don't have anything else to say. Right. and let's see if that works.net run. There we go.
Even though we have spaces, we've trimmed those spaces and we've removed those characters, those extra characters from our string of character. Very nice. Very cool. We're learning all about strings today as we learn C-Sharp.