Locked lesson.
About this lesson
We examine various String Methods to modify string variables, including changing the case and replacing parts of the string.
Exercise files
Download this lesson’s related exercise files.
10 - String Methods.docx60.8 KB 10 - String Methods SOLUTION.docx
57.8 KB
Quick reference
String Methods
String methods allow us to do different things to our strings, programatically.
When to use
Use these methods as needed.
Instructions
string name = "john";
name.ToUpper(); // convert all characters to uppercase
name.ToLower(); // convert all characters to lowercase
name.Replace("Old Thing", "New Thing"); // Replace parts
name.GetType(); // Get data type
name.Trim(); // remove trailing and preceding spaces
Hints & tips
- name.ToUpper();
- name.ToLower();
- name.Replace("Old Thing", "New Thing");
- name.GetType();
- name.Trim();
- 00:03 Okay, in this video I want to talk about string methods or
- 00:06 string functions as you might call them.
- 00:08 I call methods and functions sort of interchangeable.
- 00:11 Different programming languages refer to them as the same thing, but
- 00:14 they're basically these parentheses on thanks.
- 00:17 This will call a method or a function.
- 00:19 And we've been dealing with string so far, and we can actually do things to those
- 00:23 strings by calling certain string methods or functions.
- 00:26 So let's go ahead and create a string.
- 00:28 So I'm just going to call this myString.
- 00:32 And let's set this equal to, hello my name is john elder.
- 00:37 And we know what this is going to do if we come down here and print out myString.
- 00:44 If we save this and run it, it's just going to print out, hello,
- 00:48 my name is john elder.
- 00:50 And that's cool.
- 00:51 But what if we want to do stuff to this string programmatically?
- 00:54 Well, C-Sharp has a bunch of different built-in methods and functions that you
- 00:57 can play around with and we're going to look at just a few in this video.
- 01:00 Let's start out with To Uppercase.
- 01:02 So we notice everything here is lowercase,
- 01:04 maybe we want to change them all to uppercase.
- 01:07 How do we do that?
- 01:08 Well, we can call the .ToUpper function.
- 01:11 And you'll notice the T in to and
- 01:13 the U in upper are both capitalized and then we've got parentheses.
- 01:18 So what this will do is just simply convert all of these to uppercase letters.
- 01:22 So if we save this and run it, We get,
- 01:26 HELLO MY NAME IS JOHN ELDER in capital letters, very cool.
- 01:30 We also have ToLower and this will convert everything to lowercase.
- 01:35 Now this is already lowercase.
- 01:37 Maybe we want to come through here and say, Hello my name is John Elder
- 01:42 with a few capital letters kind of sprinkled throughout.
- 01:47 Now, if we change this to ToLower, and again, it's still a function method,
- 01:52 whatever you want to call it.
- 01:54 Save this and run it.
- 01:56 Now, notice everything has been converted back to lowercase.
- 01:59 Cool, we can use the replace method.
- 02:04 So inside of here we can go Replace and
- 02:07 then we can pass in some parameters to replace anything we want.
- 02:12 So for instance if we wanted to replace John with Tim, we could do this run it.
- 02:19 And now it says, hello my name is Tim Elder, very cool.
- 02:24 Here let's put Old and New.
- 02:29 We can use different functions and methods to get information about our string.
- 02:34 We can get the GetType.
- 02:36 So down here we can go GetType.
- 02:39 Again, it's a function, put your little parentheses, save this and run it.
- 02:45 Then it returns system.string.
- 02:47 Hey, we know that's true because we defined it right there as string,
- 02:51 very cool.
- 02:52 So what else can we do?
- 02:53 We can also .Trim.
- 02:56 So let's trim this guy.
- 02:58 And here I can put some spaces before and after, right?
- 03:02 So this actually might be kind of hard to see, but
- 03:07 if we run this, we see no spaces before or after.
- 03:11 So let's run this without the trim just to see what it looks like.
- 03:14 Save this and run it.
- 03:18 And here we see now there is spaces before and after.
- 03:22 So that .trim will remove trailing and preceding spaces,
- 03:25 which is handy for lots of different things.
- 03:27 So these are just a very few basic string methods you can play around with.
- 03:32 There are a ton of these, so you can kind of look them up online if you want sort of
- 03:37 more toys to play around with.
- 03:39 One sort of nice thing to do is using IntelliSense, we can dot,
- 03:44 we can type a dot here, and then you can see a bunch of options pop up.
- 03:49 So, you can just sort of spend some time looking through here and
- 03:52 just playing around with some of these things.
- 03:55 So, for instance, we could go straight to To and we can see there's a ToUpper,
- 04:00 ToString, ToLower, ToCararray, different things.
- 04:04 And just spend some time playing around with this and
- 04:07 seeing what's there and clicking them and seeing what they do.
- 04:12 So that's all for this video.
- 04:14 In the next video, we're going to talk about string indexing.
Lesson notes are only available for subscribers.