1
00:00:00,060 --> 00:00:06,240
Hey there, this lesson is going to be all about string methods in JavaScript, looking at trim, charging

2
00:00:06,240 --> 00:00:11,830
the case to upper, lower case, splitting, slicing, replacing, lost index of index of.

3
00:00:12,000 --> 00:00:15,510
There's a lot of options when it comes to strings covering those within the section.

4
00:00:15,960 --> 00:00:20,940
So string methods, they allow you to update and manipulate string values.

5
00:00:21,100 --> 00:00:22,760
You can search, you can slice.

6
00:00:23,010 --> 00:00:27,360
So there's a lot of things that you can do that is similar to what you can do with the race where we

7
00:00:27,360 --> 00:00:29,070
do have a paragraph here.

8
00:00:29,070 --> 00:00:30,900
So we've got a string value here.

9
00:00:31,080 --> 00:00:36,510
We can search for a particular term and then look and find the index value of that term.

10
00:00:36,810 --> 00:00:44,490
You can also see that we can get that back so we know which the index value is and where it's occurring

11
00:00:44,490 --> 00:00:45,690
within the string.

12
00:00:45,720 --> 00:00:49,890
So it's finding the first index value of its returning back, just that one.

13
00:00:50,070 --> 00:00:52,530
There's also some additional options with strings.

14
00:00:52,710 --> 00:00:55,650
And as you can see, there's quite a bit that you can do with string methods.

15
00:00:55,870 --> 00:01:00,630
There's a bunch of examples here where it's looking for index value of blue.

16
00:01:00,790 --> 00:01:04,110
So it's returning zero if it's not finding it.

17
00:01:04,320 --> 00:01:10,560
So because blue is the first value within the string, if we're looking for blue, it's going to return

18
00:01:10,560 --> 00:01:11,610
back a negative one.

19
00:01:11,640 --> 00:01:17,490
So that means that it wasn't found within the string, if we're looking for well, comma zero.

20
00:01:17,490 --> 00:01:23,760
So it's going to return back five, which is going to be the first character of Blue Whale is if we're

21
00:01:23,760 --> 00:01:28,410
looking at well, five, we're also going to be returned back five.

22
00:01:28,410 --> 00:01:33,900
So this is a parameter that you can pass in because the whale is starting at five.

23
00:01:34,080 --> 00:01:38,250
But if we start at seven, then we're going to get a negative one because it's not going to be able

24
00:01:38,250 --> 00:01:39,240
to find that within the.

25
00:01:39,840 --> 00:01:46,110
And here's another example of checking to see if Blue is contained and noticed that it is case sensitive.

26
00:01:46,380 --> 00:01:50,850
So blue lowercase is not the same as blue uppercase.

27
00:01:50,850 --> 00:01:52,710
So it's going to be returning back a negative one.

28
00:01:53,430 --> 00:01:58,250
And we're also can check to see if it doesn't exist.

29
00:01:58,260 --> 00:02:01,890
So if we're not getting a negative one because it does exist.

30
00:02:02,010 --> 00:02:02,820
So it's true.

31
00:02:02,940 --> 00:02:09,390
And as you can see, there's a number of ways to check so you can have negate it as well as equal to

32
00:02:09,390 --> 00:02:14,080
negative one and a number of different ways to check to see if something exists within a string.

33
00:02:14,430 --> 00:02:15,510
Some more examples.

34
00:02:15,720 --> 00:02:22,440
So in this example here, it displays four because E is within the fourth position index value of that

35
00:02:22,440 --> 00:02:23,760
particular string value.

36
00:02:24,000 --> 00:02:25,750
Let's try this out within our ED.

37
00:02:25,990 --> 00:02:28,770
So now let's create a string in the ED.

38
00:02:28,770 --> 00:02:31,260
And this can contain any content that you want.

39
00:02:31,440 --> 00:02:33,470
You can search for the same values.

40
00:02:33,480 --> 00:02:35,430
And I'm going to be looking for create your own string.

41
00:02:35,670 --> 00:02:40,830
And I have some numbers in there and we'll repeat JavaScript twice so I can show you that there are

42
00:02:40,830 --> 00:02:45,900
different ways if you want to return back several different words within that string.

43
00:02:45,900 --> 00:02:50,400
So refresh and we've got my string value output.

44
00:02:50,400 --> 00:02:52,890
It it is definitely a string value in there.

45
00:02:53,010 --> 00:02:55,440
We can get the length of the string.

46
00:02:55,440 --> 00:02:59,030
So doing length we can see how many characters are contained within the string.

47
00:02:59,290 --> 00:03:01,460
A lot of these all out into the console.

48
00:03:01,470 --> 00:03:06,000
So my string getting the length also trim the string.

49
00:03:06,250 --> 00:03:12,450
So if for instance we've got some spaces in the front and in the back, we can use the trim method and

50
00:03:12,450 --> 00:03:16,050
what that will do is that's going to remove out all of that white space there.

51
00:03:16,080 --> 00:03:22,650
So see that now we've got the trimmed version of it using the my string trim, whereas if we just put

52
00:03:22,650 --> 00:03:25,520
out the string, you can see that it is slightly different.

53
00:03:25,560 --> 00:03:29,130
So we've got all of the white space and that one, there's no white space in this one.

54
00:03:29,130 --> 00:03:30,270
So that's what trim can do it.

55
00:03:30,270 --> 00:03:32,520
It allows you to trim your string.

56
00:03:32,520 --> 00:03:34,190
You can also convert it.

57
00:03:34,200 --> 00:03:41,220
So converting the case so there's two uppercase to lowercase, so using to lowercase that will turn

58
00:03:41,220 --> 00:03:43,530
all of the string values to lowercase.

59
00:03:43,530 --> 00:03:47,460
As you can see in the example, you can also use several of them together if you want to.

60
00:03:47,550 --> 00:03:53,340
So trim and trimming them, as well as converting them to lower case, you can chain them together in

61
00:03:53,340 --> 00:03:53,930
that format.

62
00:03:53,940 --> 00:03:56,240
There's also, as we saw, lowercase.

63
00:03:56,310 --> 00:03:59,280
And then there's obviously there's uppercase as well.

64
00:03:59,280 --> 00:04:03,750
So we can convert it to uppercase and they'll do the same thing, except it will do the opposite.

65
00:04:03,760 --> 00:04:06,450
Return all of the string values to uppercase.

66
00:04:06,450 --> 00:04:09,030
We can also split string values.

67
00:04:09,030 --> 00:04:14,490
So we've got our string that we've been using and if we want to split it into an array, so splitting

68
00:04:14,490 --> 00:04:16,200
it by the spacing.

69
00:04:16,410 --> 00:04:19,380
So this will split out all of the words into an array.

70
00:04:19,740 --> 00:04:26,220
And this is a useful function because then you can use this in order to search out for different words

71
00:04:26,220 --> 00:04:27,390
or you can loop through them.

72
00:04:27,600 --> 00:04:32,250
And then you could apply a bunch of the array methods that are also really super useful.

73
00:04:32,490 --> 00:04:35,310
So turning a string into an array, this is a great one.

74
00:04:35,340 --> 00:04:40,110
How you're splitting up so you can split it by commas or you can split it by different letters.

75
00:04:40,410 --> 00:04:41,200
So whatever works.

76
00:04:41,200 --> 00:04:42,900
So in this case, we split it by the comma.

77
00:04:43,020 --> 00:04:45,150
If I want it to, I could take a character.

78
00:04:45,150 --> 00:04:46,770
I could split it by that as well.

79
00:04:47,160 --> 00:04:50,790
And you can see that it's got all split via the eight.

80
00:04:51,120 --> 00:04:56,160
And notice another thing to when it does the split that it actually removes the character from that

81
00:04:56,160 --> 00:04:56,510
array.

82
00:04:56,790 --> 00:04:59,730
So in this case, if we're removing the spaces, we don't.

83
00:04:59,810 --> 00:05:05,510
See any of the spaces, we just see the words, so if you want to find out which character is at index

84
00:05:05,510 --> 00:05:11,700
value nine, for example, you can see that it's going to return back R because R is in the ninth spot.

85
00:05:11,750 --> 00:05:14,360
That's the one that's returning are going to comment.

86
00:05:14,360 --> 00:05:17,730
These treat a string just as you would an array.

87
00:05:17,750 --> 00:05:23,770
So if you want to get whatever character is at nine, so just as we do character add, we can do that.

88
00:05:23,840 --> 00:05:30,020
My string nine, just getting it by the index value can also use my string to return a part.

89
00:05:30,020 --> 00:05:36,680
So we know that R is in the ninth spot and if we take the seven spot, so that should give us the W

90
00:05:36,950 --> 00:05:42,980
if we want to just return back world, we can extract that from the string using slice where we can

91
00:05:42,980 --> 00:05:44,350
have a starting position.

92
00:05:44,360 --> 00:05:46,390
And in this case we did want to start it at seven.

93
00:05:46,400 --> 00:05:50,630
So refresh and we get the string back starting at the seventh position.

94
00:05:50,660 --> 00:05:54,610
You can also specify what index value you want to stop the slide.

95
00:05:55,250 --> 00:05:58,310
So let's try 12 and we refresh.

96
00:05:58,340 --> 00:06:01,770
So that gives us the ability to pull out the word world out of the string.

97
00:06:02,180 --> 00:06:10,310
We also have another way to do this or do some similar functionality where we can select substring and

98
00:06:10,310 --> 00:06:16,190
substring returns, a subset of the string between one index and another or through the end of the string.

99
00:06:16,340 --> 00:06:22,330
So works the same way as we saw with Slice, where we're taking a substring value, where if we want

100
00:06:22,340 --> 00:06:28,670
to have a starting position of seven and an ending position of twelve, you can see it returns back

101
00:06:28,670 --> 00:06:29,480
world as well.

102
00:06:30,200 --> 00:06:31,520
There's also one more option.

103
00:06:31,530 --> 00:06:33,590
So there's quite a few options here.

104
00:06:33,590 --> 00:06:39,860
And this is a shorter format of substring, which returns back characters in the string, beginning

105
00:06:39,860 --> 00:06:43,310
at a specified location through the number of characters.

106
00:06:43,580 --> 00:06:47,580
So if we do substring 12, you're going to see that returns back 12 characters.

107
00:06:47,810 --> 00:06:54,560
So if you want to only return back world, you can use substring seven, starting position seven and

108
00:06:54,560 --> 00:06:56,390
returning back five characters after that.

109
00:06:56,390 --> 00:06:57,590
And that returns McWorld.

110
00:06:57,860 --> 00:07:03,050
So there are slight differences to these and depending on what the situation is, you can use either

111
00:07:03,050 --> 00:07:05,870
one in order to accomplish what you're trying to find.

112
00:07:06,580 --> 00:07:09,010
You can also replace content within the string.

113
00:07:09,290 --> 00:07:14,750
So if we want it to, for instance, outputs and I'm going to create another one where we're just going

114
00:07:14,750 --> 00:07:19,730
to output these values into the console, because I know we've got quite a lot going on in this console.

115
00:07:20,090 --> 00:07:25,970
So setting up and I'm going to have a variable called output and we're just going to declare that for

116
00:07:25,970 --> 00:07:26,360
now.

117
00:07:26,510 --> 00:07:33,440
And then within the code, we're going to set a value of output and then logout output into the console

118
00:07:34,220 --> 00:07:35,260
and we'll just output that.

119
00:07:35,270 --> 00:07:37,720
So that's our current string that we're using right now.

120
00:07:38,300 --> 00:07:43,870
And if we want it to replace content within the string, we can use the replace method.

121
00:07:44,150 --> 00:07:47,930
So it looks for a pattern within a string and then does the replacing.

122
00:07:48,140 --> 00:07:49,670
So this is another really useful one.

123
00:07:49,700 --> 00:07:56,840
So if you want to look for the word love and change it to enjoy, so maybe you find you don't really

124
00:07:56,840 --> 00:07:58,910
love JavaScript, you enjoy JavaScript.

125
00:07:59,150 --> 00:08:03,770
So it will rework the string and it will update those values.

126
00:08:04,070 --> 00:08:06,530
So you can set it as well to my string if you wanted to.

127
00:08:06,560 --> 00:08:11,900
So if you could update the current string or you can create a new variable to hold the new value so

128
00:08:11,900 --> 00:08:17,360
that we can get the first index value and we can also get the last index value, the string object,

129
00:08:17,360 --> 00:08:21,720
an index of and looking for the index of.

130
00:08:21,740 --> 00:08:26,120
And in this case, let's do JavaScript, because we do have two of those words in there.

131
00:08:26,270 --> 00:08:33,600
We can see that the starting position of JavaScript is 13, whereas if we want to do last index of this

132
00:08:33,600 --> 00:08:40,160
whole return, the last occurrence of the word, so we just put last in front of index of and there

133
00:08:40,160 --> 00:08:43,520
should be uppercase and refresh and you can see the last index.

134
00:08:43,700 --> 00:08:47,120
So this JavaScript is starting at position forty seven.

135
00:08:47,390 --> 00:08:53,270
So you can use these in combination as well, where you can slice at a word if you want, if you know

136
00:08:53,270 --> 00:08:59,780
it's going to be nine characters and you can get the index starting index value using any one of these

137
00:08:59,780 --> 00:09:04,040
index of or last index, you can also search within the index.

138
00:09:04,040 --> 00:09:07,880
So output is going to be equal to my string and search.

139
00:09:08,030 --> 00:09:13,520
So it executes a search for a pattern between a regular expression and a string object so we can search

140
00:09:13,520 --> 00:09:14,660
for a particular word.

141
00:09:14,870 --> 00:09:19,520
In this case, we just use JavaScript together because it makes a lot of sense within this course.

142
00:09:19,530 --> 00:09:24,680
We're searching and we're getting the same result as we did with index of because it's returning the

143
00:09:24,680 --> 00:09:27,230
index position of that search result.

144
00:09:27,350 --> 00:09:31,880
We're going to look at these in more detail as well later on within the section.

145
00:09:32,030 --> 00:09:37,370
When we look at regular expressions, it don't go anywhere yet with this lesson's not concluded.

146
00:09:37,490 --> 00:09:42,050
I want you to try out the string methods that we've been showing you within this lesson to turn a string

147
00:09:42,050 --> 00:09:46,010
into an array of words, replace the content, update the output of the string.

148
00:09:46,040 --> 00:09:46,790
Have some fun.

149
00:09:46,910 --> 00:09:49,160
Try out the different string methods that we've covered.
