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
€92.78
List Price:  €129.90
You save:  €37.11
£79.40
List Price:  £111.16
You save:  £31.76
CA$136.11
List Price:  CA$190.56
You save:  CA$54.44
A$154.13
List Price:  A$215.78
You save:  A$61.65
S$135.08
List Price:  S$189.12
You save:  S$54.03
HK$782.28
List Price:  HK$1,095.23
You save:  HK$312.94
CHF 90.61
List Price:  CHF 126.85
You save:  CHF 36.24
NOK kr1,085.23
List Price:  NOK kr1,519.37
You save:  NOK kr434.13
DKK kr692.01
List Price:  DKK kr968.84
You save:  DKK kr276.83
NZ$167.80
List Price:  NZ$234.94
You save:  NZ$67.13
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳10,976.08
List Price:  ৳15,366.96
You save:  ৳4,390.87
₹8,339.52
List Price:  ₹11,675.66
You save:  ₹3,336.14
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦141,842.81
List Price:  ₦198,585.61
You save:  ₦56,742.80
₨27,810.04
List Price:  ₨38,935.18
You save:  ₨11,125.13
฿3,647.70
List Price:  ฿5,106.92
You save:  ฿1,459.22
₺3,232.12
List Price:  ₺4,525.11
You save:  ₺1,292.98
B$499.21
List Price:  B$698.91
You save:  B$199.70
R1,908.54
List Price:  R2,672.04
You save:  R763.49
Лв180.65
List Price:  Лв252.92
You save:  Лв72.26
₩135,197.71
List Price:  ₩189,282.20
You save:  ₩54,084.49
₪368.63
List Price:  ₪516.10
You save:  ₪147.47
₱5,633.91
List Price:  ₱7,887.71
You save:  ₱2,253.79
¥15,144.47
List Price:  ¥21,202.86
You save:  ¥6,058.39
MX$1,659.40
List Price:  MX$2,323.22
You save:  MX$663.82
QR364.31
List Price:  QR510.04
You save:  QR145.73
P1,370.91
List Price:  P1,919.33
You save:  P548.42
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,729.52
List Price:  E£6,621.52
You save:  E£1,892
ብር5,680.63
List Price:  ብር7,953.11
You save:  ብር2,272.48
Kz83,612.74
List Price:  Kz117,061.18
You save:  Kz33,448.44
CLP$97,978.20
List Price:  CLP$137,173.40
You save:  CLP$39,195.20
CN¥722.95
List Price:  CN¥1,012.16
You save:  CN¥289.21
RD$5,921.50
List Price:  RD$8,290.34
You save:  RD$2,368.83
DA13,490.83
List Price:  DA18,887.70
You save:  DA5,396.87
FJ$226.12
List Price:  FJ$316.58
You save:  FJ$90.46
Q779.86
List Price:  Q1,091.83
You save:  Q311.97
GY$20,923.51
List Price:  GY$29,293.76
You save:  GY$8,370.24
ISK kr13,946.60
List Price:  ISK kr19,525.80
You save:  ISK kr5,579.20
DH1,013.19
List Price:  DH1,418.51
You save:  DH405.32
L1,763.34
List Price:  L2,468.75
You save:  L705.40
ден5,702.11
List Price:  ден7,983.18
You save:  ден2,281.07
MOP$805.89
List Price:  MOP$1,128.28
You save:  MOP$322.39
N$1,893.44
List Price:  N$2,650.90
You save:  N$757.45
C$3,681.15
List Price:  C$5,153.75
You save:  C$1,472.60
रु13,335.63
List Price:  रु18,670.42
You save:  रु5,334.78
S/370.84
List Price:  S/519.19
You save:  S/148.35
K382.72
List Price:  K535.82
You save:  K153.10
SAR375
List Price:  SAR525.01
You save:  SAR150.01
ZK2,522.76
List Price:  ZK3,531.96
You save:  ZK1,009.20
L461.43
List Price:  L646.02
You save:  L184.59
Kč2,350.75
List Price:  Kč3,291.15
You save:  Kč940.39
Ft36,729.02
List Price:  Ft51,422.10
You save:  Ft14,693.08
SEK kr1,071.30
List Price:  SEK kr1,499.86
You save:  SEK kr428.56
ARS$85,766.82
List Price:  ARS$120,076.98
You save:  ARS$34,310.16
Bs691.04
List Price:  Bs967.48
You save:  Bs276.44
COP$387,583.68
List Price:  COP$542,632.66
You save:  COP$155,048.97
₡50,832.34
List Price:  ₡71,167.31
You save:  ₡20,334.97
L2,468.78
List Price:  L3,456.40
You save:  L987.61
₲737,805.73
List Price:  ₲1,032,957.54
You save:  ₲295,151.80
$U3,781.90
List Price:  $U5,294.82
You save:  $U1,512.91
zł400.73
List Price:  zł561.05
You save:  zł160.31
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.