1 00:00:00,120 --> 00:00:04,010 Next up, we move on to another string function called replace. 2 00:00:04,019 --> 00:00:09,930 So replace allows us to replace portions of a string with some other replacement string. 3 00:00:10,110 --> 00:00:15,330 Now, like all of these functions I've shown you so far, we're not actually updating the data, at 4 00:00:15,330 --> 00:00:17,090 least the way that we're using them right now. 5 00:00:17,100 --> 00:00:19,200 We're not updating our tables at all. 6 00:00:19,200 --> 00:00:25,890 We're simply displaying or selecting information and changing the way that information looks after we 7 00:00:25,890 --> 00:00:28,110 remove it or we select it from the database. 8 00:00:28,110 --> 00:00:30,390 But our data is completely unchanged. 9 00:00:30,390 --> 00:00:35,010 And that's important to note, because some people think, you know, the word replace means that we 10 00:00:35,010 --> 00:00:39,540 are replacing the contents of our table, that we're actually updating rows. 11 00:00:39,540 --> 00:00:42,570 But that's not the case, at least with what I'm showing you right now. 12 00:00:43,050 --> 00:00:49,440 So as always, I recommend pull up the docs, find the examples, replace or replace. 13 00:00:49,440 --> 00:00:50,100 There you are. 14 00:00:50,130 --> 00:00:51,360 It's a pretty simple one. 15 00:00:51,360 --> 00:00:54,900 We have to provide three different string arguments. 16 00:00:54,900 --> 00:00:58,920 The first one is the string that we will be operating on. 17 00:00:59,040 --> 00:01:04,860 The second one is what we want to replace and the third one is what we want to replace it with. 18 00:01:05,310 --> 00:01:08,700 So in this example, this is the initial string. 19 00:01:09,000 --> 00:01:15,480 This is what we're trying to replace and we're replacing it with an uppercase W, lowercase W So instead 20 00:01:15,480 --> 00:01:21,450 of w w w we now have upper w, lower w, upper W lower, upper and lower. 21 00:01:21,600 --> 00:01:23,790 So let's take a look at an example here. 22 00:01:23,790 --> 00:01:27,150 What do you think I end up with when I run this line? 23 00:01:27,570 --> 00:01:30,240 Select Replace Hello world. 24 00:01:30,910 --> 00:01:34,420 Hell and then four characters. 25 00:01:34,780 --> 00:01:37,940 Well, remember, this is what we are operating on. 26 00:01:37,960 --> 00:01:39,670 This is what we're replacing. 27 00:01:39,700 --> 00:01:44,950 So this h e l will be replaced with this last argument. 28 00:01:45,220 --> 00:01:46,900 So we get this. 29 00:01:48,210 --> 00:01:50,190 BLEEP o world. 30 00:01:50,220 --> 00:01:53,250 Let's run it just to prove it over in my shell. 31 00:01:53,250 --> 00:01:54,240 I'll paste it in. 32 00:01:54,240 --> 00:01:55,140 And there we are. 33 00:01:55,140 --> 00:01:57,720 We see bleep o world. 34 00:01:58,350 --> 00:02:01,110 Okay, now, how about this one here? 35 00:02:01,110 --> 00:02:02,010 I'm on two lines. 36 00:02:02,010 --> 00:02:03,240 Just because it was too long. 37 00:02:03,240 --> 00:02:06,360 The font was tiny, but there's no reason to do it on two lines. 38 00:02:06,360 --> 00:02:06,840 Really. 39 00:02:07,170 --> 00:02:09,870 Replace cheese, bread, coffee, milk. 40 00:02:10,580 --> 00:02:17,090 And then what are we trying to replace a space character and what are we replacing every space with? 41 00:02:18,280 --> 00:02:20,170 SpaceX and SpaceX. 42 00:02:20,440 --> 00:02:26,050 So essentially, any time there is a SpaceX, it's going to be replaced with SpaceX and SpaceX, meaning 43 00:02:26,050 --> 00:02:29,350 we end up with cheese and bread and coffee and milk. 44 00:02:30,700 --> 00:02:32,440 I'll prove that one as well. 45 00:02:32,860 --> 00:02:33,700 Paste it in. 46 00:02:33,700 --> 00:02:34,450 And there we are. 47 00:02:34,480 --> 00:02:37,060 Cheese and bread and coffee and milk. 48 00:02:37,480 --> 00:02:41,470 So that's all there is to using replace, at least on its own. 49 00:02:41,470 --> 00:02:44,500 Right, without a table, without any real data behind it. 50 00:02:44,560 --> 00:02:47,470 It still works the same way, but there's no fancy options. 51 00:02:47,470 --> 00:02:48,920 There's no different ways of calling it. 52 00:02:48,940 --> 00:02:51,700 You always have to have those three values. 53 00:02:51,700 --> 00:02:56,800 The initial string, the thing you're trying to replace and the thing you want to replace it with. 54 00:02:56,830 --> 00:02:59,480 If so, those three values must always be there. 55 00:02:59,500 --> 00:03:03,700 And another thing you should know is that it is case sensitive. 56 00:03:03,730 --> 00:03:05,080 Let's do an example here. 57 00:03:05,080 --> 00:03:07,000 Let's select replace. 58 00:03:07,780 --> 00:03:09,310 Whoops, I accidentally hit enter. 59 00:03:09,310 --> 00:03:10,570 You know, that's going to be confusing. 60 00:03:10,570 --> 00:03:14,480 Then we just terminate the string and clear my screen and start this over. 61 00:03:14,500 --> 00:03:15,490 All right, go away. 62 00:03:15,620 --> 00:03:17,890 Control, By the way, we'll clear the screen. 63 00:03:17,890 --> 00:03:19,750 So select replace. 64 00:03:20,200 --> 00:03:22,660 I am cold. 65 00:03:22,690 --> 00:03:23,220 How about that? 66 00:03:23,230 --> 00:03:25,110 I'll put an exclamation point there too. 67 00:03:25,120 --> 00:03:27,670 And then I want to replace my name. 68 00:03:27,670 --> 00:03:33,490 So if I do it this way and I don't capitalize it correctly and I replace it with, I don't know, Mr. 69 00:03:33,490 --> 00:03:35,050 Steel like that. 70 00:03:36,130 --> 00:03:36,970 We still see. 71 00:03:36,970 --> 00:03:37,720 I am Colt. 72 00:03:37,720 --> 00:03:39,150 It's completely unchanged. 73 00:03:39,160 --> 00:03:40,750 Again, the casing matters. 74 00:03:40,750 --> 00:03:46,180 If I try and replace see Capitol Colt, then it works. 75 00:03:46,180 --> 00:03:47,320 I am Mr. Steel. 76 00:03:47,830 --> 00:03:50,740 So let's try an example now with our books table. 77 00:03:51,430 --> 00:03:53,050 What should we do with our books? 78 00:03:53,050 --> 00:03:54,490 Select star from books. 79 00:03:54,490 --> 00:04:01,660 We've got these spaces in the names of a lot of the books Norse mythology, American Gods, Interpreter 80 00:04:01,660 --> 00:04:02,560 of Maladies. 81 00:04:02,560 --> 00:04:07,270 Why don't we try replacing all spaces with hyphens or dashes? 82 00:04:07,270 --> 00:04:12,430 So I'm going to do a select replace the title of every book. 83 00:04:12,430 --> 00:04:14,170 We want to replace a space. 84 00:04:14,170 --> 00:04:15,400 Let me use single quotes. 85 00:04:16,360 --> 00:04:18,579 And replace it with a dash. 86 00:04:18,940 --> 00:04:21,680 I don't know why we want to do that, but let's say that's what we care about. 87 00:04:21,700 --> 00:04:26,170 And I'm an idiot because I didn't say from books. 88 00:04:27,190 --> 00:04:30,680 And there we are, the namesake Norse mythology, Right? 89 00:04:30,700 --> 00:04:32,640 What we talk about when we talk about love. 90 00:04:32,650 --> 00:04:34,360 Great short story collection, by the way. 91 00:04:34,360 --> 00:04:37,540 Anyway, lots of spaces replaced with dashes. 92 00:04:38,140 --> 00:04:39,490 So that's one example. 93 00:04:39,520 --> 00:04:41,100 We can get even fancier. 94 00:04:41,110 --> 00:04:42,580 I'm not going to bother with it right now. 95 00:04:42,580 --> 00:04:44,910 I'll show some more complicated examples later. 96 00:04:44,920 --> 00:04:49,510 But we could do the same thing that we did where we combined cat and substring. 97 00:04:49,510 --> 00:04:51,730 We could combine replace with any of them. 98 00:04:51,730 --> 00:04:58,300 I could replace all the spaces with a hyphen and then concatenate or we could concatenate the first 99 00:04:58,300 --> 00:05:04,960 name and the last name and then replace spaces like David Foster Wallace has a space in his last name, 100 00:05:04,960 --> 00:05:06,970 but I think I want to keep this moving along. 101 00:05:06,970 --> 00:05:08,470 So that's it for Replace. 102 00:05:08,470 --> 00:05:10,000 Just remember the way that it works. 103 00:05:10,000 --> 00:05:11,320 Three arguments. 104 00:05:11,320 --> 00:05:12,610 All of them have to be there. 105 00:05:12,610 --> 00:05:15,040 The first one is the piece of text we're operating on. 106 00:05:15,070 --> 00:05:19,570 The second one is what we're trying to replace, and the third one is what we want to replace it with. 107 00:05:19,570 --> 00:05:27,400 And I can't stress enough, we have not altered anything in our table if I do a select title from books. 108 00:05:29,380 --> 00:05:30,240 Nothing has changed. 109 00:05:30,250 --> 00:05:31,690 We still have spaces there. 110 00:05:31,690 --> 00:05:34,240 We're not updating the table at any point. 111 00:05:34,270 --> 00:05:41,170 All that we did was retrieve the titles and replace them once we had retrieved and replace the spaces 112 00:05:41,170 --> 00:05:42,070 with dashes.