Check out a free preview of the full C# and .NET Basics course
The "Strings" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:
Spencer discusses various methods and techniques for declaring and manipulating strings in C#. He also explains important string methods like IsNullOrEmpty, IsNullOrWhiteSpace, Equals, Length, StartsWith, EndsWith, Trim, Substring, and Replace. Additionally, they mention the use of the StringBuilder class for more efficient string manipulation.
Transcript from the "Strings" Lesson
[00:00:00]
>> Spencer Schneidenbach: Okay, so we've talked about some built-in types, I did wanna move back to strings a little bit and kinda just talk through a few of the methods that I think are really important for declaring strings. As I mentioned, they are immutable by nature, which means you can't change them, you can reassign the variable, but you can't change them once they're instantiated.
[00:00:19]
There's a few different ways to declare strings which I think all of them bear mentioning. So if I have a string name Spencer like this, this is just a string declared with a double quote on one side and a double quote on another. So this has a few properties that you should be aware of, if you try to put in a forward slash, it's gonna tell you, wait a second, what the heck is that?
[00:00:44]
It's gonna say, hey, it's just gonna throw a syntax error, this is an escape character. So you would do this to, for instance, write a another single quote, or, sorry, another double quote inside of the string. This character would escape that character so that you could put it in that string without it, it thinks that that's the end of the string.
[00:01:03]
There's a couple of examples of this such as escape character n or escape character t being a new line, t being tab respectively, so that's how you declare kind of a quote on quote normal string. There's also a verbatim string which does not have a concept of escape character, so you can write as many slashes as you want, or even slash n and and it won't interpret that as a new line within that string.
[00:01:31]
Another kind of property of this is that, because you can't escape double quotes, a double quote does a single double quote tell me if that's confusing. A single double quote throws a syntax error and the way that you write a double quote, an actual double quote is to just put it twice inside of the context of a verbatim string.
[00:01:52]
Next, I wanna briefly mention a relatively new string type of way of declaring a string, which is a string literal, I was very excited when [INAUDIBLE] got this. And it will basically be pretty much anything you want, including double quotes, including lots of double quotes, and it will interpret all of these things, as a just a normal string.
[00:02:17]
Really good for if you're testing a blob of JSON or something and you just wanna drop in and don't wanna worry about the double quotes kind of messing you up, they are multi-line by nature, which is really cool. So another kind of string thing to have in your toolbox, and then lastly there is what's called string interpolation.
[00:02:41]
So if you wanted to say name Spencer, and then you wanted to greet Spencer, you could say string greeting, and then a you can put in that name variable inside of your string. You would start do that by doing dollar sign, and then you would have quotes, hello, which would be the normal part of the string.
[00:03:01]
And then you would use a curly brace to say name, and that would give you a string with hello, Spencer written in that. So, few important ways to declare strings, couple of things that I want to mention, you can declare an empty string with a double quote, like this.
[00:03:19]
String, empty string, double quote, semicolon, I don't choose to do this most of the time, I know this is very common in other platforms, especially JavaScript where you just have empty strings everywhere like that. I prefer to use string.empty just for consistency, and as you can see, if we dig into the source code for this, let's see, it's a static, okay, I'm not sure where it's actually instantiated, but it is essentially the value of a blank string, it is the exact same as saying this just that string.
[00:03:54]
Another thing that I use a lot, a couple of more methods that are on the string class, that I think are really important to talk about, is this is an example of a static method on the string class, which is is null or empty and is null or whitespace.
[00:04:08]
So this is a method that returns a bool and it basically says that is this an empty string or is this a null string? An example of something that would be is null or empty, string is null or empty, if you passed it in an empty string, it would return true, if you passed it in a string with whitespace, it would return false.
[00:04:33]
There's another method which is the one that I use a ton of which is null or whitespace, I do this because in the context of web applications. Sometimes people like to put trailing and leading spaces inside of text fields, so I wanna be able to check for those.
[00:04:45]
And so the way that I would do that is by using is null or whitespace, and we'll check to see if the string is null, is empty or is whitespace. And it's basically just looking for the absence of a meaningful value to the application, something that I use a lot.
[00:05:02]
Last thing that I will mention is a string equality, so there's a couple of different ways to determine whether a string is equal, you can use the equal operator, or so if you wanted to do an if statement to say, is name double equals. And again, it doesn't have the same coercion semantics that it does in JavaScript, it's a triple equals in JavaScript essentially.
[00:05:28]
If name equals greeting like this, you can do that and that will perform a case sensitive comparison. I actually prefer not to compare strings like this, I like to do it more explicitly using the static method string., sorry string.equals. And what that will give you is it gives you a few overloaded methods to tell whether two strings are equal and I wanna point out one of the two overloads.
[00:05:56]
So this is if you wanted to check the strings for equality, you could also use double equals in this case. But in almost all cases where I'm doing any kind of string comparison, I will specify the string comparison explicitly. And the ones that I use the most are ordinal or ordinal ignore case, the other one has to do with culture, which is not something that, frankly, I use a lot of in terms of I don't deal a lot with cultural differences in applications.
[00:06:24]
So most of the time for most developers, they are writing ordinal ignore case, if we had Spencer and Spencer, that would put those two strings as being equal. I prefer this vastly over what developers like to do, which is name.ToLower, which would of course return a lower string and then greeting.ToLower.
[00:06:50]
We're getting a little bit ahead, but if this name or this greeting are instances that are null, if they're set to null, it will throw an exception and we can see that if we run run this program and accept, I set string name = null, and then we run this program.
[00:07:07]
You'll see that in the console we will get a null reference exception, which we will talk about when we get into talking about types a little bit more. So I do like to use the static string.equals method because it will handle milk gracefully and it won't throw an exception.
[00:07:25]
So word to the wise out there, there's a bunch of things about strings that a lot of different methods that you can use to operate and modify strings. You can check the length of a string by doing the greeting.length, and you can know how long that string is, which is useful in case you were working with a database where it's like this thing can only be 100 characters and you wanna limit that string to 100 characters.
[00:07:53]
You can also use methods like starts with or ends with to see if a string starts with a specific string of characters. So we could see that greeting starts with sp en you know that, greeting.starts with which returns Boolean, we can see that here. Bool, you can use that to check to see if a string starts or ends with a specific thing.
[00:08:19]
You can trim the string, which means removing the whitespace from the beginning and the end. You can take a portion of that string using the substring method, and you can also replace parts of that string. So if you wanted to replace, let's say, one of my clients lovingly refers to me as Spencer Rita, but sometimes he calls me margarita.
[00:08:48]
So if I wanted to play replace my greeting.Replace and we wanted to replace the greeting or yes with Spencer Rita with meow, that's how you would do that. So I'd highly encourage you to look through this documentation and kinda get familiar with the string methods as you're kind of working with them.
[00:09:14]
And I highly encourage anybody to check the documentation that is built right into the IDE, right? You can hit greeting.and then see all of the methods that are available on the string class, so that string's in a nutshell. Last thing I will mention is that every time you create a new string, you allocate a new string which there could be performance implications of that if you find that you're doing that.
[00:09:41]
StringBuilder is a class if you're having to append or assign a string and build upon a string, maybe you're in a loop and a hundred times it runs and every time it adds to a specific StringBuilder is a way that you can gain some efficiency. It's a common pattern, I also like it cause it's super explicit and it doesn't do the string allocation, most of the time you don't have to worry about that performance, but I do use string builder a lot, just in my day to day.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops