WEBVTT

00:00.180 --> 00:06.630
The next Pilton string function that we'll work with is called replace and what it does is it replaces

00:06.660 --> 00:08.700
parts of a string.

00:08.700 --> 00:14.130
So if you want to get rid of a certain occurrence of a character or you wanted to replace all spaces

00:14.130 --> 00:19.840
with commas or something like that you could use replace and the syntax looks like this.

00:20.130 --> 00:27.580
Here's an example where we have the string hello world and you know what I just realized this again.

00:27.750 --> 00:30.160
That's a bad word right there.

00:30.660 --> 00:37.260
So if we want to get rid of that we could use Replace hello world comma.

00:37.290 --> 00:39.190
What we're trying to remove.

00:39.450 --> 00:44.560
And then what we want to replace it with and it will do that to all occurrences.

00:44.580 --> 00:47.270
In this case hell only occurs once.

00:47.640 --> 00:49.740
So that's what our output looks like.

00:50.140 --> 00:52.720
Percent star Octa Thorpe.

00:52.860 --> 00:54.160
Oh what's this thing called.

00:54.300 --> 01:00.400
There's an actual name for the at symbol but there's a real name and then O world.

01:00.420 --> 01:04.120
So if I copy that over and just run it now.

01:04.380 --> 01:06.810
See that's the output that we get.

01:06.810 --> 01:17.350
We could also do something like replace hello world and let's just replace all Elle's with sevens.

01:17.370 --> 01:18.920
Why not.

01:19.020 --> 01:21.680
And that's what we need to do there.

01:22.110 --> 01:27.390
And then we get hello world but with sevens instead of ells you know we could do the same thing just

01:27.390 --> 01:34.740
to show you one more with oh's and replace them with zero.

01:34.740 --> 01:38.150
Now one thing that is important to pay attention to.

01:38.460 --> 01:45.670
Let's say I have hello here with a capital O.

01:46.050 --> 01:46.520
Hello.

01:46.520 --> 01:52.620
Here with a lowercase Oh excuse me world with lower case so the idea just say have upper and lower case

01:52.640 --> 01:53.570
oves in here.

01:53.650 --> 01:58.070
If I try and replace 0 and then say I just want to replace it with.

01:58.170 --> 02:00.220
That's an asterisk.

02:00.240 --> 02:01.810
What do you think will happen.

02:03.550 --> 02:09.140
And the answer is it only replaces exactly this character so it is case sensitive.

02:09.140 --> 02:12.020
So it didn't touch this capital O here.

02:12.140 --> 02:17.250
So that's one way to use your place the other way that I mentioned is that it can replace multiple things.

02:17.270 --> 02:23.960
So here is a civic example where I have a string cheese space bread space coffee space milk.

02:23.960 --> 02:32.240
Maybe it's a grocery list or something and I want to replace all spaces with the string and wall space

02:32.270 --> 02:33.410
and space.

02:33.680 --> 02:40.640
Basically the output will be cheese and bread and coffee and milk inserted that right there.

02:40.640 --> 02:44.060
Here here and here every time there's a space.

02:44.210 --> 02:48.640
So if I tried running that you can see it works.

02:48.650 --> 02:50.460
Cheese and bread and coffee and milk.

02:50.840 --> 02:53.680
So now let's do something with the book data we have.

02:53.690 --> 02:56.970
We'll do something really simple and similar to what we just did.

02:57.010 --> 03:04.490
Let's replace all E's with threes and we'll just do lowercase E's So anytime there's a lowercase like

03:04.730 --> 03:10.090
namesake Norse American we'll replace it with three.

03:10.160 --> 03:13.950
So to do that is a select replace and don't ask why.

03:14.120 --> 03:19.860
It's not really a real world example but it's like a place you want to do title.

03:20.480 --> 03:26.340
And then what are we trying to replace lowercase is what do we want to replace him with three.

03:27.020 --> 03:30.200
And then what table from books.

03:30.510 --> 03:38.310
And we could get more specific but let's just do from books and you can see where you get the name sake

03:38.330 --> 03:41.760
with three threes and all the way down.

03:41.840 --> 03:45.520
Consider the lobster Cannery Row and so on.

03:45.680 --> 03:51.650
And that's pretty much it to using replace a bit of a bonus at the end here is that also you can combine

03:51.650 --> 03:57.530
replace with other functions like we've already seen use concat and substring together so we could try

03:57.530 --> 04:01.430
the same thing by using replace and Concat for instance.

04:01.430 --> 04:08.030
So I could do something like If I work in this book code I will comment this out which I haven't shown

04:08.030 --> 04:08.840
how to do yet.

04:09.130 --> 04:16.220
Highlighted it and on a Mac its command slash on a PC I believe it is control slash.

04:16.990 --> 04:23.030
And when it commented out which means it just won't run it Tirso I can see it but sequel treats it are

04:23.030 --> 04:26.400
minuscule treats it as if there's nothing there when it runs it.

04:26.420 --> 04:29.780
So what I want to do now I want to combine replace.

04:29.870 --> 04:36.740
So what I'm going to do is select and I'm just going to fill in some rough blanks here so we have a

04:36.740 --> 04:41.030
place and then we're going to have from books.

04:41.480 --> 04:46.820
But what we want to do is not just replace things I'll do the same thing where we replace all ease with

04:46.850 --> 04:55.810
threes so that replace title the lower case e with three like that.

04:56.060 --> 05:01.420
But I'm only going to take the first 10 characters like we did with substring.

05:01.640 --> 05:11.720
So I need to do substring and I want to do substring of this whole thing.

05:14.380 --> 05:20.300
And you know I'm realizing as this goes on this is a little convoluted so I'll throw up another Don't

05:20.300 --> 05:21.280
panic here.

05:21.320 --> 05:27.920
This is more just me showing you that it's something you can do but it's not essential that you wrap

05:27.920 --> 05:31.760
your head around this entirely but just to show you so substring.

05:31.910 --> 05:35.600
And the first argument is replace this whole thing.

05:35.600 --> 05:44.630
So basically we're taking a substring of this and then if this and we want to go from 1 to 10 so that's

05:44.630 --> 05:45.310
a lot there.

05:45.340 --> 05:54.230
Now if I pasted in here you can see what it did was first replace always with three and then take the

05:54.320 --> 05:58.400
substring of that from 1 to 10.

05:58.790 --> 06:02.010
And of course we could give it a better name because it is pretty ugly.

06:02.120 --> 06:11.540
So we could do as weird string let's say and now we have weird string appear and yeah that's pretty

06:11.540 --> 06:11.840
much it.

06:11.850 --> 06:14.270
I just wanted to show that we could combine them together.

06:14.480 --> 06:16.790
Why you would do this exactly again.

06:16.820 --> 06:18.060
You probably wouldn't.

06:18.170 --> 06:23.670
But the key here is that they're kind of like Lego pieces you can reorder them you can.

06:23.930 --> 06:29.640
Well maybe more like nesting dolls actually where you can nest them inside of one another.

06:29.840 --> 06:34.940
So like we did substring inside of a concat we can do a replace inside of substring.

06:34.940 --> 06:39.490
So that's enough of a place we're going to continue with a couple more and then we'll do some challenges.
