Last time, we learned about the string datatype and some of the foundational ideas behind strings, and as a reminder, strings are sequences of characters. In this lecture, we'll learn some more interesting ways to extract information from strings. So what might we want to do with a string? We might want to convert a string to a value type, which is what we do whenever we parse the string. We might want to find a particular character in the string, we might want to extract a piece of a string and put it into a new string, and you may discover there are other things you need to do with strings, and if you do, you should read the string documentation to discover how to do those things. Let's go explore these ideas in a little more detail in code. The work we'll do in our code here is we'll read in a comma-separated value or CSV string, we'll find the location of the comma in that string, we'll extract the name and percent from our comma-separated values string, and we'll print the name and percent to make sure that we extracted them properly. We'll start by reading in the CSV string, so we'll need to prompt the user to enter a CSV string, and because we expect the user to provide their input in a specific format, we should tell the user what format they should use, so we'll tell them name, percent, and now we'll read in their input and we know how to do this, we'll have a string variable that I'll call CSV string and we'll just do a console read line to put the user input into the CSV string. The next thing we need to do is find the common location and the way we would discover how to find the common location is we'd open up the string documentation and we'd look for a method that would help us find the location of a specific character in the string. We'll look at the string documentation soon but I already know the answer to this question, it's the index of method that we need to use. We know we're going to put the common location into an int because the indexes of each character in the string are, of course, ints so, common location will be an int and as you can see, I can almost just accept what IntelliSense suggested except I'm not going to do that, I'm going to just say look for the comma. IndexOf is an instance method, it's not a static method, so we use our CSV string variable and we call the IndexOf method on it and we provide the character that we're looking for. Just temporarily, let's print out comma location just to make sure we're finding the location properly. I run my code, and I'll say Bob, 0.1, and we get three as the common location. If that seems strange to you, remember we use zero-based indexing as we program, so the b character is at index 0 and o is at index 1, and b is at index 2, so the comma is at index 3. You might also wonder what the IndexOf method returns if there is no comma and it returns negative 1 if it can't find the comma. Theoretically, before we use this comma location, we should do some error checking to make sure that the user actually provided their input in the correct format. Because we're focusing on the string processing, in this lecture, we'll just assume the user has provided their input in the correct format. Now we know where the comma is, how do we extract the name and percent? It turns out that we want to extract pieces of the string, specifically, we want to extract the piece of the string that's before the comma for the name and we want to extract the piece of the string after the comma for the percent. Now, let's go take a look at the string documentation and I'll tell you, I've already brought us to the appropriate place in the string documentation, we're looking at the sub-string method. There are actually two overloads for the substring method. For the first overload, we provide a single int. Int 32 in the documentation is the same as int, and the substring that we get back starts at a specified character position and continues to the end of the string. That's not what we want to try to extract name. We don't want to go all the way to the end of the string. We want to start at the beginning of the string and only go with the number of characters that are actually in the name. That means to extract the name we need the second overload of the substring method, where we provide two integer arguments. We provide the specified character position as the start of the substring and we provide the length of the substring. Now you might think we should provide the ending character position of the substring we want, but it turns out that providing length is actually much more convenient for us and we'll talk about why as soon as we go back to the code, which we're doing now. We know that the name will be a string and we'll do substring. In fact, IntelliSense suggested exactly what we should do. We want to start at the very first character in the string, that will be the B in our Bob comma or whatever example, and the length of the substring we want to extract is actually commaLocation. It works that way because we're using zero-based indexing. The length of Bob is three characters and commaLocation is three. We can just provide commaLocation here as the length of the substring we want to extract. That works because we're using zero-based indexing. Let's print the name so we can make sure we're extracting the name properly. I know I'll want a blank line. I'll still do Bob. Bob's getting marginally smarter, but we don't care, we're just extracting the name. We extracted Bob correctly. The next thing we need to do is extract the percent. We know the percent will be a float, so we want to store the percent in a floating point value. It would be easier if we just stored the percent in a string as well, but that would be incorrect. We should store the percent as the datatype that the percent really is. In this example we're not going to do anything with percent, but we might decide that we wanted to add 10 to the percent so everyone gets a letter grade boost in their percent. To do that math, we'd have to store percent as a float. It's always best to store data in our programs as the datatype that they truly are rather than out of programming laziness, trying to make it as easy as possible. We're going to use substring again, but we're going to use the other overload of substring where we started at a particular location and go to the end of the string because the percent goes from just after the comma to the end of the string. Again, IntelliSense has suggested just what we need here. We don't want to start at commaLocation because then we'd include the comma in our percent. We need to start at comma location plus 1. Of course, we're getting a compilation error here. Our error is we can't implicitly convert type string to float. But we've seen this before and we know what to do. We know that this is a string and we know how to convert a string to a value type like a float. We'll use float.Parse and I'll hit "Enter" so we can see the entire line of code here when I'm done. We can parse this substring into a float and put it into our percent variable. We've seen float.Parse, we've seen int.Parse. There's actually a bool.Parse and a short.Parse There's lots of parses for converting strings into value types. We'll go ahead and confirm that we're extracting the percent properly as well. Bob's getting really smart now. His percent will be 101.3 percent. We've extracted the name properly, which we already knew, and we've extracted the percent properly as well. This idea of being able to process a comma separated value string is really useful to us. We can export CSV string data from lots of different spreadsheet programs and then we could actually process that data here in our console apps or even our Unity games after we learn about file input and output, which will be in the next course in the specialization. But being able to take a string that's a comma separated value string and extract the data from that string is really a useful skill to have. Knowing how to use the index of and substring methods to do that is really helpful. To recap, in this lecture we learned we can use a variety of string methods to do useful string processing.