Last time we talked about the string data type, and just as a reminder, a string is simply a sequence of Unicode characters. This time we're going to look at a couple of more interesting processing things that we can do with strings. As a reminder, here are the module learning objectives for Module Six. And we're really working on the second one, in this case. Where we're taking a look at string processing. So you might ask yourself, what might we want to actually do with strings? We've already seen how we can read them in using console.read line. We've already seen that we can convert them into a value type. We did that last time. We might need to look for a particular character in the string. So for example, it is very common for people to export CSV files, comma separated value files, from Excel or something like that. You could also separate them by tabs. You can use any separator character you want when you export. So it might be the case, though, that we want to write a program that reads in such a file and finds the locations of those separators so that we can do interesting stuff. So we might have to find a particular character. And in fact, once we've found that particular character, we might want to extract part of that string into a new string. To, to sort of gather the information from each line in that CSV file. And we might do something else. And that's so vague, right? What something else? Whatever you discover you need to do with strings. And the way we move forward as programmers. In, programming, in general, and game development, in particular. Is, we can't learn everything as beginners. We learn what we need. And then, as we need to learn new stuff, we learn it. And so, you may discover lots of things you want to do with strings. And when the time comes, you'll look at the documentation and you'll try to figure it out. Okay, so let's actually start with an in-lecture quiz where I ask you how we can do that find a particular character in the string, and then we'll actually go do some code to do it. I will open up the project that I already created. >> [SOUND] >> And we'll say, read in CSV string. Prompts to remember are console.right with no politeness whatsoever. Enter name, well okay, we can't use Worte. It has to be right. Enter name and percent. So imagine this is for a class or something like that. And it makes, if we're expecting a particular format from the user, even though we know users are going to mess with us no matter what, we should tell them the format. So let's say, name, percent like that. So there's out prompt; and we'll read in the CSV string using console read line. Now, once we have a comma separated string, we can extract values from that string and the first thing we're going to do is find out where the comma is, so [NOISE] find comma location and we can do that, let me just declare a variable here. [NOISE] We can do that by using, hopefully you've got this right on the induction quiz we can use that, you do that using the index of method. So all we have to do is say csvstring.IndexOf and we'll look for the comma without any extra parenthesis, hopefully. Okay. And, by the way, I did this as a string but we can, of course, look for a single character as well. By using single quotes around the literal rather than double quotes around the literal. So let's actually use the debugger to explore a little bit more how indexOf works by setting a break point, and I'll run it. And first I'll enter something like My last name or name Bob, and my percent, I'm stupid, so we'll say zero. Think for a moment about what you think indexOf will return and then we can look at comma location and it's three. So, remember we start at zero, character zero is B, character one is O. Character two is B and the comma is at location three. The other thing we can do, I'll press F5 and that will just finish the program for me. I'm going to, and notice by the way that because I'm using the debugger in this particular case, I'm actually using F5. In this console application instead of Cltr+F5. That's why I said when I, that's why when I said go all the way to the end, it actually just flashed that screen. Okay, I'm going to do it one more time and then I'm going to enter something without any comma at all. And when we get to the break point. When we get to the break point, the comma location is negative one. So if indexOf finds what we're looking for, it returns the location of the first occurrence of what we're looking for. So if we'd had multiple commas in our string it wouldn't matter, it still would give us the location of the first one. The other thing that happens is, if what we're looking for does not appear in the string, then it just tells us, well it returns negative one, okay. So now we know where I'm going to get rid of the break point, and I'll F5 to finish off. So now we've found the location let's extract. [NOISE] Name and percent. We'll also print name and percent and then of course that blank line that we know and love. Okay before we extract. What we need to extract, go to the in lecture quiz to tell me if you have a good guess about that. All right. So, we actually extract using sub-string, and the sub-string takes a piece of the string we're extracting from it and puts it into a new string. Remember, strings are immutable, so we can't modify the string we have, but we can create new strings from it. So I'll say string name is equal to, and now we need to use sub string on our csv string, to extract a piece of the string. Now we've talked about overloads. Sub string has two overloads. One, We give it the Start Index and it goes all the way to the end. It starts at whatever we've provided as that Start Index. Remember, the thing between those square brackets when we accessed a single character. In the sequence of characters that are the string. Is called an Index. So we can give it the Index. And it'll go all the way to the end of the string but that's definitely not what we want here. What we want here is we want to start at the beginning of the string and go up to the comma. So we're going to use the otherr over overload. We're going to use the overload where we provide a start index and a length. So, the start index is zero, because we want to start extracting the name from the beginning of this string, and the question is how long? How many characters do we need to grab. And it turns out that having zero based strings and having index of work the way it does is awesome because the length is actually comma location. We want to extract Three characters. The B, the O, and the B, from the example that I used. So, I'm going to print this one to show that we've extracted that properly before we move on. So the name is that. We will Control+F5 now because I'm not debugging anymore. I'll still do Bob. I'll do ten now because bob's getting smarter over time. And the name is Bob. So it extracted it properly using sub string. The next thing we want to do is let's extract that percent. Now you might be tempted to say string percent because I know I'm doing string processing. But that would be wrong. It would be wrong morally. You're a bad person. Don't do that. So, in fact percent is a float, so let's store percent as a float. We'll do csvstring substring. And this time we actually want that first overload. Where we start at a particular place and go the end of the string. And that particular place is not comma location, it's comma location plus one. And you might find that you need to actually like use those archaic things, pencil and paper. And write down a string and look at it, and number all of the characters starting at zero. And see why it needs to be comma location plus 1. You should check that out. However, I am getting a compilation error here, and I can't implicitly convert string to float. You should say, wow I remember seeing that last time. Because if I want to convert a string to a numeric data type. Then I need to parse it, and this time it's not an int it's a float but that's okay, we'll just do float parse instead of int parse. And we'll print it out again, well, not again [LAUGH] we'll print this out for the very first time but we will print out the percent as well. Just to demonstrate that we have, in fact, extracted it properly. Like so. And so Control+F5. I'll enter Bob comma and this time I'll do 20.0 because Bob realizes a percent should be a float as well. And it prints out Bob and 20, and we're in good shape. Okay. So here's the thing. We have, and, and that's really it. That's the exploration of the string processing that we're going to do for today. Index of. There's a corresponding method called last index of, and substring are three methods that you are likely to use to process strings regularly to do the stuff you need. When you discover there's something else you need to do to strings, you go read the string documentation. Each method tells what it does. And you find the one that will work for you. So that's it for our string module, Module Six. So next time in the next lecture we will move on to Module Seven. Where we will start talking about how we can make decisions in our programs.