1 00:00:01,190 --> 00:00:06,170 Welcome back to learn to code in JavaScript in this lecture, we're going to talk about strings and 2 00:00:06,170 --> 00:00:08,290 get a little bit more of a formal introduction. 3 00:00:09,770 --> 00:00:14,770 So we've seen strings already and we've seen like Hello World in quotes and that sort of thing. 4 00:00:15,980 --> 00:00:18,140 But let's talk in a little more detail. 5 00:00:18,170 --> 00:00:23,780 So strings contain a list of UTF 16 characters, which is a Unicode character set. 6 00:00:24,500 --> 00:00:30,620 And all that means is that it takes up two bytes or maybe four bytes depending. 7 00:00:31,460 --> 00:00:35,630 And on the character set contains ABCDE. 8 00:00:35,660 --> 00:00:36,980 It contains one, two, three. 9 00:00:36,980 --> 00:00:39,650 It contains assigned pound sign. 10 00:00:40,040 --> 00:00:41,570 It contains Chinese characters. 11 00:00:41,570 --> 00:00:43,010 It contains Arabic characters. 12 00:00:43,010 --> 00:00:45,950 It contains different wingdings and things like that. 13 00:00:47,730 --> 00:00:55,560 And each character has a numeric representation, so every character in a string is represented by a 14 00:00:55,560 --> 00:00:56,070 number. 15 00:00:56,880 --> 00:01:03,750 So if you were in such a such a frame of mind, you could do some sort of arithmetic with those numbers 16 00:01:03,930 --> 00:01:05,370 to come up with different strings. 17 00:01:06,120 --> 00:01:08,940 So strings also support a variety of methods. 18 00:01:09,450 --> 00:01:14,040 They can operate on the string, produced new strings or give results about strings and information. 19 00:01:16,580 --> 00:01:22,830 Some of these methods are static, which means that you use them by saying string dot something. 20 00:01:24,080 --> 00:01:26,630 So an example, string dot from CHAKO. 21 00:01:26,960 --> 00:01:28,640 Now, most of these are not very useful. 22 00:01:29,150 --> 00:01:35,590 So what this is saying is, given the character codes of 65, 66, 67, give me a string. 23 00:01:37,100 --> 00:01:39,170 Not that useful in most cases. 24 00:01:40,490 --> 00:01:43,720 So most useful methods are what they call instance methods. 25 00:01:44,300 --> 00:01:48,200 And this is where you provide a function on the string itself. 26 00:01:49,610 --> 00:01:52,130 So something like you have a string hello world. 27 00:01:52,700 --> 00:01:58,790 You say DOT index, which is one of the instance methods in which you're looking for is a substring 28 00:01:58,790 --> 00:02:08,450 that says world in that's going to be there and that's going to evaluate to six because strings are 29 00:02:08,450 --> 00:02:09,470 zero based. 30 00:02:09,920 --> 00:02:18,050 So H is that position zero G as it is a position one, the first Elorza position to second at three 31 00:02:18,740 --> 00:02:24,950 O at four, the space at five and the W and world is at six. 32 00:02:29,210 --> 00:02:35,690 There's one really useful instant property, which is length to say how long a string is, so you can 33 00:02:35,690 --> 00:02:39,920 say something like Hello World that length and of course hello, world in quotes can be replaced by 34 00:02:39,920 --> 00:02:40,520 a variable. 35 00:02:42,110 --> 00:02:43,580 So that evaluates to 11. 36 00:02:49,090 --> 00:02:55,720 So as I said, it could be a string literal or it could be a variable like my string that like. 37 00:02:59,440 --> 00:03:04,930 And then we have something called string templates, so a string template has this little funny back 38 00:03:04,930 --> 00:03:11,980 type character, which on your average Windows keyboard is above the tab next to the one. 39 00:03:13,090 --> 00:03:16,150 So you put the back to character and it's like another type of quote. 40 00:03:16,750 --> 00:03:21,010 But what you can do is put in strings into the template. 41 00:03:21,490 --> 00:03:30,820 So we have a variable called name and we and we use this dollar curly brace to differentiate it so that 42 00:03:30,820 --> 00:03:35,020 it replaces that spot with what's the value of the variable name. 43 00:03:35,500 --> 00:03:35,800 All right. 44 00:03:35,810 --> 00:03:39,310 So let's look at some selected methods that will do some examples of. 45 00:03:41,060 --> 00:03:48,920 So we have a concat method which concatenate strings, and so if we have a string that has hello and 46 00:03:48,920 --> 00:03:55,830 another string that has world, we can say astar concat, a space comma string to. 47 00:03:56,030 --> 00:03:57,950 So we end up with Helo's based world. 48 00:03:58,610 --> 00:04:03,290 Now there's another little concatenation device, which is the plus side, and you can see it right 49 00:04:03,290 --> 00:04:07,190 here is that the plus sign can also concatenate strings. 50 00:04:07,670 --> 00:04:13,250 So I can say the string is plus some other string and we'll concatenate them together. 51 00:04:14,030 --> 00:04:19,610 And a third way to keep Cartney strings is to use that template string template with a little back tick 52 00:04:20,390 --> 00:04:23,080 quotation mark that we talked about just a second ago. 53 00:04:24,880 --> 00:04:30,160 And then we have some pairs of things like, for example, have starts with and ends with, so this 54 00:04:30,160 --> 00:04:34,720 returns true if the string either starts with the string or ends with the string. 55 00:04:35,650 --> 00:04:41,470 So in this case, in the example it's saying, I have a constant string equals Hello world, and then 56 00:04:41,470 --> 00:04:47,100 I'm going to say in the council that, Lord, does the string end with world. 57 00:04:47,560 --> 00:04:48,730 If it does, it's true. 58 00:04:50,060 --> 00:04:52,340 It starts with just looks at the beginning instead of the end. 59 00:04:53,950 --> 00:04:56,440 And then we have a method called includes. 60 00:04:57,420 --> 00:05:04,470 And in this case, we're just checking to see if the substring is included in the mainstream, in the 61 00:05:04,470 --> 00:05:07,110 includes, you can also specify a starting position. 62 00:05:09,380 --> 00:05:16,250 Index of and last index is going to give us the index of a substring, so remember, the index is starting 63 00:05:16,250 --> 00:05:16,930 at zero. 64 00:05:17,690 --> 00:05:23,330 And so in this case, the example, it's going to find the index of world. 65 00:05:23,780 --> 00:05:29,680 Now, if world was in the string of multiple times, it would only find the first one last index up, 66 00:05:29,690 --> 00:05:30,700 finds the last one. 67 00:05:31,140 --> 00:05:35,730 And in this case, you can also specify a position to start within the string. 68 00:05:36,170 --> 00:05:42,070 So if you had 10 worlds in the string, you can say, find me the first one and start after that position 69 00:05:42,080 --> 00:05:43,430 and finally the next one and so on. 70 00:05:44,920 --> 00:05:52,990 Pad starting pattern, this is really handy for formatting things, it says go ahead and pad a string 71 00:05:52,990 --> 00:05:55,750 with a certain character to get it up to a certain length. 72 00:05:56,650 --> 00:06:02,740 So pads start in this example, it's going to pad hello world with asterisks. 73 00:06:03,310 --> 00:06:06,070 So that is a 24 character length string. 74 00:06:07,490 --> 00:06:13,040 So sometimes you do it with spaces so you can have things formatted right, justified or not justified. 75 00:06:14,380 --> 00:06:24,520 More method's repeat repeats the string, so hello, world, here it is, repeat five times, such as 76 00:06:24,640 --> 00:06:26,860 say hello world, hello world whole world five times. 77 00:06:28,990 --> 00:06:32,680 Replace and replace all depending on which one you pick. 78 00:06:33,160 --> 00:06:38,160 It's going to either replace the first occurrence of a substring or replace all of the occurrences. 79 00:06:38,950 --> 00:06:44,200 So in this example, string that replace little old with Bego, it's going to just replace the first 80 00:06:44,200 --> 00:06:44,440 one. 81 00:06:46,490 --> 00:06:53,000 Slice, if you think about it, that's what it's doing, it's slicing a substring from the original 82 00:06:53,000 --> 00:06:53,420 string. 83 00:06:54,380 --> 00:07:03,650 When I say slice three, it's going to slice off the ACL and leave the Yellow Space or Helda as a substring 84 00:07:04,700 --> 00:07:11,090 split splits a string on a separator, and often you have a comma separated string or something like 85 00:07:11,090 --> 00:07:11,240 that. 86 00:07:11,250 --> 00:07:15,500 But in this case, I've just got HTC, that, et cetera, world. 87 00:07:16,160 --> 00:07:23,690 And when I do a split, it creates an array of the characters split on that string. 88 00:07:24,260 --> 00:07:25,270 And we'll look at this more and more. 89 00:07:25,280 --> 00:07:26,270 We're talking about arrays. 90 00:07:27,930 --> 00:07:35,460 Substring pulsated substring from the original string, so starting in a position for a certain length, 91 00:07:36,270 --> 00:07:38,670 get a substring out of that original string. 92 00:07:40,540 --> 00:07:48,330 To strength, this is Maxim, kind of not useful in the string area, but all objects have a two string 93 00:07:49,060 --> 00:07:50,140 and we'll talk about that more. 94 00:07:50,140 --> 00:07:54,610 So in this example, I'm converting the number one, two, three to a string. 95 00:07:55,610 --> 00:07:57,620 To lower case and to upper case. 96 00:07:58,560 --> 00:08:00,960 Upper cases are lower cases, the entire string. 97 00:08:02,900 --> 00:08:05,000 From start to amend or trim. 98 00:08:05,950 --> 00:08:11,230 Will trim white space from the start or the end or both ends of the string, depending on which one 99 00:08:11,230 --> 00:08:11,720 you choose. 100 00:08:13,250 --> 00:08:19,820 So in the next election, let's look at some examples of using some of these methods and the length 101 00:08:20,360 --> 00:08:22,610 and a few of the operators on strength's.