1 00:00:00,290 --> 00:00:03,979 Applications that require users to sign up and sign in often 2 00:00:03,979 --> 00:00:08,720 have unique constraint requirements for passwords and other form fields. 3 00:00:10,910 --> 00:00:15,475 Password fields can require verification that certain characters are included in 4 00:00:15,475 --> 00:00:16,590 a password string. 5 00:00:17,700 --> 00:00:22,133 You may need to determine if an email address ends with a specific 6 00:00:22,133 --> 00:00:23,620 string like dot edu. 7 00:00:24,790 --> 00:00:29,216 This small piece of a larger string is called a substring. 8 00:00:29,216 --> 00:00:35,245 JavaScript provides strings with multiple options for searching for substrings. 9 00:00:35,245 --> 00:00:38,474 The first method we'll explore is indexOf. 10 00:00:38,474 --> 00:00:43,378 The indexOf method returns the position of the first occurrence 11 00:00:43,378 --> 00:00:45,930 of a specified value in a string. 12 00:00:47,730 --> 00:00:53,470 If the substring is present, the method returns the index where it occurs. 13 00:00:53,470 --> 00:00:58,559 If the substring is not present, the method returns the value -1. 14 00:01:00,029 --> 00:01:03,640 Let's try this out in the workspace associated with this video. 15 00:01:05,457 --> 00:01:08,165 Open up a preview of the index.html 16 00:01:08,165 --> 00:01:12,598 file associated with this project by clicking the eye icon. 17 00:01:15,147 --> 00:01:19,820 For this step, we'll be working with the code in the search.js file. 18 00:01:21,140 --> 00:01:24,230 There, we have a string with the value team. 19 00:01:25,420 --> 00:01:30,986 In the console of our preview, let's run the indexOf method on index string. 20 00:01:41,522 --> 00:01:46,264 We'll pass it a value i to see where i occurs in the spring. 21 00:01:47,532 --> 00:01:50,400 We get -1 because there's no i in team. 22 00:01:51,600 --> 00:01:56,221 Now let's try to find the index of tree in our school variable that stores 23 00:01:56,221 --> 00:01:57,672 the value treehouse. 24 00:02:11,444 --> 00:02:15,505 Because the substring starts at the beginning of the string, 0 is returned. 25 00:02:17,345 --> 00:02:20,296 Strings also have a method called lastIndexOf. 26 00:02:20,296 --> 00:02:21,985 Can you guess what this method does? 27 00:02:23,770 --> 00:02:25,554 LastIndexOf searches for 28 00:02:25,554 --> 00:02:30,670 a substring starting from the strings end instead of the first character. 29 00:02:30,670 --> 00:02:34,507 For example, 30 00:02:34,507 --> 00:02:43,790 school.indexOf("e") returns 2. 31 00:02:43,790 --> 00:02:48,448 But school.last indexOf("e"), 32 00:02:55,249 --> 00:03:00,002 Returns 8, because the e at the eighth index position is 33 00:03:00,002 --> 00:03:03,180 the first e from the end of the string. 34 00:03:04,260 --> 00:03:06,810 Let's clear up the console before we go any further. 35 00:03:08,526 --> 00:03:11,533 IndexOf tells us where a substring is, but 36 00:03:11,533 --> 00:03:15,550 sometimes we only want to know if a substring is present. 37 00:03:16,700 --> 00:03:20,582 For this, we can use the handy includes method. 38 00:03:20,582 --> 00:03:27,383 Includes takes a search string and an optional index to start searching. 39 00:03:27,383 --> 00:03:32,651 It returns true if the search string is present and false if not. 40 00:03:32,651 --> 00:03:37,966 Let's replace indexOf in one of our previous examples with includes. 41 00:03:40,602 --> 00:03:47,737 We'll try school.includes("tree"). 42 00:03:50,983 --> 00:03:52,190 And we get the value true. 43 00:03:53,670 --> 00:03:58,508 We can use the starts with and ends with methods to check for 44 00:03:58,508 --> 00:04:02,678 substrings at either the start or end of strings. 45 00:04:02,678 --> 00:04:04,639 We'll clear the console again. 46 00:04:08,301 --> 00:04:15,413 In our search.js, we have a variable named childrensStory that stores a value, 47 00:04:15,413 --> 00:04:19,960 Once upon a time not long ago, and ends with the end. 48 00:04:21,720 --> 00:04:26,049 For practice, we'll write a program that checks if strings meant 49 00:04:26,049 --> 00:04:30,080 to be fairy tales start with the substring, Once upon a time. 50 00:04:31,920 --> 00:04:38,203 We'll take our childrensStory variable, use the startsWith method. 51 00:04:40,483 --> 00:04:43,453 And pass it the value once upon a time. 52 00:04:51,447 --> 00:04:52,740 We get a value of true. 53 00:04:53,920 --> 00:04:58,027 Now let's check that the story ends with the proper, the end. 54 00:04:58,027 --> 00:05:01,300 Again, we'll use our childrensStory variable. 55 00:05:04,324 --> 00:05:09,080 EndsWith passing it a value of the end. 56 00:05:12,104 --> 00:05:13,150 And we get true again. 57 00:05:14,240 --> 00:05:18,793 In the next video, I'll share a set of methods you can use when you want to 58 00:05:18,793 --> 00:05:22,840 modify a string value, and return a new object with that value.