In this movie, we're going to talk about string conditionals. There are certain unique situations when you're comparing strings in an if statement or another type of conditional that we need to consider. Now, you may remember from a previous movie, we talked about the object wrapper that is associated with strings, and we use the length property to determine how many characters were in a string. Well, there are also methods with this object wrapper that is associated with strings, and it helps us work with strings that can help us in the case of conditionals, that can help us in other situations as well, we're going to take a look at what some of those methods are. We'll be doing this in the console. So I'm going to open up the console.
And let me give you a scenario that we want to use for this example. Let's say you've created program where you're testing young children with certain questions and the question you ask is what color's the sky? And so they're supposed to respond with blue. Well, you put that ant, sir, that they provided into variable. And let's go ahead and and let's go ahead and assume that the child entered blue in all lowercase letters. That's how the child answered.
And so that's what our variable currently contains. Now, how can we write an if statement determine if the answer is correct. If we write something like this, we'll use our equality operator. But if we're testing against uppercase blue is it going to come out indicate that the answer is correct. So we're if it is, we will console that log correct. Otherwise in the else clause, we will console dot log.
Wrong. Obviously, you'd probably probably want to use more friendly feedback, but this will get the point across. All right, so that's our statement. So what do you Is it going to indicate it's correct or wrong? Well, let's go ahead and press return to find out. It indicates it's wrong.
You probably guessed, because blue with an uppercase B is different than blue with a lowercase b. So how do we deal with that? Well, one of the methods it's available with the object wrapper for the string is to lowercase so let's modify our if statement slightly Remember, we use the dot notation when we're accessing something that's a part of an object. In this case, we're accessing a method that's a part of the object wrapper for strings. And the method we want to access is to lowercase. Since it is a method, we need to indicate the parentheses in order for it to execute.
Now, this will take this string and convert it to all lowercase characters no matter how it was entered. And so that means we need to compare it with all lowercase characters. Now let's see what happens. And we get the response that it's correct. Now there's a similar method but it converts it to uppercase. In this case, we would need to compare it with all uppercase press return, obviously we get correct.
And so that's an example of two methods that are part of the of the string object that allows us to work with conditionals. Now let's assume that the answer that was entered is now this. So uppercase B, but there's also a space after the B, they typed a space. After entering the statement, well is our current. If statement going to work, I'm pressing the up arrow key to retrieve it again from the console. And now I'm going to press return.
And it tells us that it is wrong because there is a space at the end of the string so the string is no longer the same. All right, there's another method we can use to help with that. This is called the trim method. What trim method does is it removes all leading and trailing spaces. Now notice how I'm entering this trim method. I have answer.to uppercase dot trim.
This is called chaining. I'm I'm chaining methods together. The reason this works is because answer is a string. And this method is going to act on that string and do what it does, it converts everything to uppercase. When this method is done, it returns another string. And so this method, the trim method will act on that string do what it does and returns a string.
The string that is returned is then compared against the value. All right, let's see what we get this time. And we get that it's correct because it removes that trailing space. So another way to work with strings when we're doing comparisons of this nature. Alright, let's add another wrinkle to this. Let's say the child entered We asked them what color is the sky and they assumed they needed to respond in a full sentence.
And so they entered, the sky is blue. That was their answer. Now, even the trim methods not going to work for so I can press the up arrow key, bring that back, press return, and obviously it indicates that it's wrong. So that's not going to work for us anymore. So how do we deal with that? Well, there is another method.
It's called index of. And let me go ahead and modify the if statement. First. We still want to convert it to uppercase. We're going to remove the trim method I'm going to put index of. Now what index of does is it searches through searches through the string and finds the occurrence of whatever string we enter.
I'm going to enter Blue so it's going so answer is the sky is blue. It converts it all to uppercase so the sky is blue all all uppercase, and returns a string then index of searches through that finds the occurrence of blue and returns a number. It returns the index of that number. So let me scroll up here to the sky is blue. So the T and that would be zero this is zero based. Remember, there are many things in programming that are zero based strings are zero based.
So, t is zero H is 123. The spaces 3456789 1011 is where blue starts 11 1213 1411 is where blue starts. So that should return 11. Now, if index of does not find blue, in the stream, it will read It will return a negative one. So how can we make use of that? Well, if it doesn't find blue and returns a negative one, then we know it's wrong.
If blue was nowhere in the answer, then they did not get the color, right? So we know it's wrong. so we can simply test greater than negative one. We don't want it to be a negative one. If it's a negative one, it's wrong. And so let's see what we get when we press return here.
It tells us it is correct. Okay, so you can see how that is working. Let's go ahead and do answer.to uppercase dot index of blue. See what that returns it returns 1111 is the character position where blue starts. And so that's what it returns. Let's do this again.
Answer to uppercase. Now let's do index of read this time and see what that returns a negative one because it cannot find read in the string. And so that's how you could use index one to help solve that problem. All right, a couple of more methods we want to look at, let's say we need to extract from a stream, a portion of that stream. So I'm going to create a new string here, str equals oops abcdefg H I J, K. Okay. There is a slice method with the string object, and slice takes two numbers.
The first number is the starting character. So let's say we wanted to extract ABCD. So we enter zero as a starting character, the second number is the ending character. Now the ending character does not get extracted everything before the ending character gets extracted. So if we wanted to extract ABCD, we would enter the character number four E, because that does not get extracted. That would be 01234.
So we would enter four and that returns ABCD. What if we wanted to get e f, g, h? That would be force is where he starts we already counted that out. But what's the ending character? For 5678 would be the ending character because we don't extract it. We extract the character before up to the character before the ending character so so we enter an eight press return and that gives us EF g h There's another extraction method that's similar to slice.
It's substring or su B str. There's actually a substring method as well, but I'm going to introduce you to this one. Now the way substring works su B str, the way that works is you get the starting character and then you get the length. So if the starting character is we want to get ABCD. Again, starting character would be zero, the length would be four. However, it's reading that four differently from when we use slice.
Okay, but that's it returns ABCD. Now what if we wanted to get EF g h? Well, the starting character is four. But once again, the length is four. So we don't know in slice we used eight because we had to specify the ending character with es su bsts. Our method, the substring method, we just indicate the length of the string, E, F, G, H. Now see that could be useful with the answer issue we were dealing with.
If we wanted to retrieve the answer the color, we could do it like this answer dot s. UB str. Well, let's do it this way. Sorry. Let's make it a little bit easier for us. Let's create another variable first. And we'll set that equal to uppercase index of blue.
Then I've entered this before and so it was already available for me to select. Then we do answer dot slice. Okay, so this is an example using slice, answer dot slice would do the position and the position plus four. All right, let's see what With that return, see if that returns blue. Sure enough, it does. What if we wanted to do that with su B str, so I'm going to come over here.
We started that position, but the length of blue we know is four already. So we don't have to determine the last index, which is what we had to do. With slice, we had to use the pause that the position variable, and then add four to it. To determine the last position, we can simply enter the length with substring, and we get blue again. So those are some methods that are available with strings. Let's review those really quickly.
So to lowercase it just converts the string to lowercase. So you enter the variable that contains the string or the string itself.to lowercase using our dot syntax to uppercase Same thing, but converts it all uppercase characters trim removes, removes leading and trailing spaces from a string. Index up and in for index, we have to put inside the parentheses the string we're looking for. It finds the index of that string, this the character position of the first character of the string we entered. And it returns that last index i was very similar to to index up but it returns the very last position. If the string exists in more than one place.
Slice it returns a portion of the string, we must enter a starting position. So the character we want to start extracting with and then we enter can enter the end position. It does not extract the end position but it extracts up to the character before the end position. Now the end position is optional. If we do not enter it, it just strap, it just extracts to the end of the string. Okay, we also looked at SU B str substring substring, I still call it substring.
We have a starting character there and then we enter a length once again, the length of length is optional. If we do not include it, it extracts to the end of the stream. So those two are a little bit different. I'd mentioned there's also a substring. And that's why I've been calling the previous method s UB str, because there is a substring method. It is very similar to the slice method, it functions almost exactly the same.
The only difference is that slice allows you to enter a negative number. If you enter a negative number, then that is relative to the end of the string. So it starts looking for a position from the end of the string. And so that's really the main difference between slice and substring. They basically function similar. So those are some methods available with the string object wrapper that you can use in situations where you're testing a condition, but also in other situations if you find the need to.
That was a long movie, but these are important concepts. Alright, let's move on to the next topic.