Locked lesson.
About this lesson
We explain the concept of String Indexing to manage and modify elements within a string.
Exercise files
Download this lesson’s related exercise files.
11 - String Indexing.docx60.9 KB 11 - String Indexing SOLUTION.docx
57.8 KB
Quick reference
String Indexing
String indexing allows you to know how many characters are in your string, and allows you to manipulate those characters.
When to use
Use it whenever you want to determine how many characters are in your string.
Instructions
string myName = "John Elder";
myName[0];
myName.Remove(5); // removes everything after the 5th item in myName
myName.Length; // get the length of the string
Hints & tips
- myName[0];
- myName.Remove(5);
- myName.Length;
- 00:03 Okay, in this video I want to talk about string indexing.
- 00:07 And string indexing is determining how many things are in your string.
- 00:12 So how many characters, and manipulating them based on that number.
- 00:17 And there's all kinds of different reasons why you might want to use
- 00:19 something like this.
- 00:20 And it's actually pretty easy to do.
- 00:22 So let's just jump in here and create a string and
- 00:25 we'll call it myString again and set that equal to my name is John Elder, all right.
- 00:33 And if we want to print this out, we can.
- 00:38 If we save this and run it really quick,
- 00:39 we know that this is going to return my name is John Elder.
- 00:42 Well, with a string, each character is a number and
- 00:47 they start at zero, so m is zero, y is one,
- 00:52 this space is two, n is three, and so on and so on.
- 00:57 And we can reference them using square brackets, and
- 01:00 we could just pass in the number that we want.
- 01:02 So for instance, if we say print out the zeroth item of our string,
- 01:07 zeroth index number, we could do that.
- 01:10 If we save this and run it, it should just return M.
- 01:12 And sure enough, it does, M, right?
- 01:16 So okay, why is that useful?
- 01:18 Well, like I said, there are lots of times when you need to
- 01:21 know how many characters are in your string.
- 01:24 Maybe you want to replace certain things, maybe you want to remove certain things.
- 01:29 We talked about in the last video different string functions.
- 01:33 There is a remove function, right?
- 01:36 So we can call that and say .remove and then inside of here we can
- 01:42 pass in a number so 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
- 01:47 So we say, hey, everything after the tenth item in our index, remove that.
- 01:51 So if we save this and run it, we see my name is and nothing else.
- 01:57 So that's kind of cool we might need to know the length so L-E-N-G-T-H.
- 02:03 Now this actually doesn't require parentheses so we can .length,
- 02:08 type this down.
- 02:09 And this will return how many characters this string is, so
- 02:14 save this and run it, and we see 21, very cool.
- 02:19 And now let's confirm this.
- 02:22 So let's go 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
- 02:27 21, right?
- 02:29 So the length of this is 21.
- 02:32 The index number doesn't start with one.
- 02:35 You see how I started counting?
- 02:37 Same thing with remove, the index number always starts at zero but for
- 02:40 counting purposes we count like normal, which is a little bit confusing, right?
- 02:44 So like I said, this is 21 characters.
- 02:47 This r is the 20th index number, though, right?
- 02:51 That's a little confusing.
- 02:52 Why is that?
- 02:53 Because like I said it starts at 0, so M is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
- 02:58 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, right?
- 03:03 So there are 20 index numbers, but 21 characters, if that makes sense.
- 03:09 So that's string indexing,
- 03:11 just sort of remember it's a way to find out how many characters in your string and
- 03:15 how to do things inside the string based on the number.
- 03:19 Like I said, we can remove everything after the 10th item,
- 03:22 so my name is, everything else gets removed, and that's all there is to it.
- 03:26 So in the next video we're going to look at string concatenation and interpolation.
Lesson notes are only available for subscribers.