Working with String Methods in Conditionals

Modern JavaScript Using Control Structures
16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.