The next built in string function that we'll work with is called replace.
And what it does is it replaces parts of a string.
So if you wanted to get rid of a certain occurrence of a character or you wanted to replace all spaces
with commas or something like that, you could use replace and the syntax looks like this.
Here's an example where we have the string hello world.
And you know what?
I just realized this again.
That's a bad word right there.
So if we wanted to get rid of that, we could use replace helloworld comma, what we're trying to remove
and then what we want to replace it with.
And it will do that to all occurrences.
In this case, Hal only occurs once.
So that's what our output looks like.
Percent Star Octave Thorpe.
Oh, what's this thing called?
There's an actual name for it.
The at symbol.
But there's a real name and then O World.
So if I copy that over and just run it now, you can see that's the output that we get.
We could also do something like Replace Hello World and let's just replace all L's with sevens.
Why not?
And that's what we would need to do there.
And then we get hello world but with sevens instead of L's, you know, we could do the same thing just
to show you one more with O's and replace them with zero.
Now, one thing that is important to to pay attention to.
Let's say I have hello here with a capital oh.
And hello here with a lowercase o or excuse me, world with lowercase.
So the idea is just I have upper and lowercase o's in here.
If I try and replace O and let's say just want to replace it with, let's do an asterisk.
What do you think will happen?
And the answer is it only replaces exactly this character.
So it is case sensitive.
So it didn't touch this capital.
Oh, here.
So that's one way to use replace.
The other way that I mentioned is that it can replace multiple things.
So here's a silly example where I have a string cheese space, bread, space, coffee, space, milk,
maybe it's a grocery list or something.
And I want to replace all spaces with the string and well, space and space.
Basically the output will be cheese and bread and coffee and milk.
It inserted that right there.
Here, here and here.
Every time there's a space.
So if I try running that, you can see it works.
Cheese and bread and coffee and milk.
So now let's do something with the book data we have.
We'll do something really simple and similar to what we just did.
Let replace all ease with threes and we'll just do lowercase e.
So any time there's a lowercase e like namesake, North American will replace it with a three.
So to do that is a select replace and don't ask why.
It's not really a real world example, but select replace.
We want to do title and then what are we trying to replace?
Lowercase is what do we want to replace them with?
Three.
And then what?
Table from books.
And we could get more specific.
But let's just do from books.
And you can see we get the namesake with three threes and all the way down.
Consider the Lobster Cannery Row and so on.
And that's pretty much it to using replace a bit of a bonus at the end here is that I'll show you we
can combine replace with other functions like we've already seen.
We use canned cat and substring together so we could try the same thing by using replace and canned
cat for instance.
So I could do something like if I work in this book code, I will comment this out, which I haven't
shown how to do yet.
Highlight it.
And on a mac it's command slash on a PC.
Believe it is control slash.
So I'm going to comment it out, which means it just won't run it here so I can see it.
But SQL treats it or my SQL treats it as if there's nothing there when it runs it.
So what I want to do now, I want to combine, replace.
So what I'm going to do is select and I'm just going to fill in some rough blanks here.
So we have replace and then we're going to have from books.
But what we want to do is not just replace things.
I'll do the same thing where we replace all e.s with threes.
So that's replaced title.
The lowercase e with three like that.
But I'm only going to take the first ten characters like we did with Substring.
So I need to do substring and I want to do substring of this whole thing.
And you know, I'm realizing as this goes on, this is a little convoluted.
So I'll throw up another don't panic here.
This is more just me showing you that it's something you can do.
But it's not essential that you wrap your head around this entirely.
But just to show you so substring.
And the first argument is replace this whole thing.
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 a lot there.
And now if I paste it in here, you can see what it did was first replace all ease with three and then
take the substring of that from 1 to 10.
And of course, we could give it a better name because it is pretty ugly.
So we could do as weird string, let's say.
And now we have weird string up here and yeah, that's pretty much it.
I just wanted to show that we could combine them together.
Why you would do this?
Exactly.
Again, you probably wouldn't.
But the key here is that they're kind of like Lego pieces.
You can reorder them.
You can.
Well, maybe more like nesting dolls, actually, where you can nest them inside of one another.
So like we did substring inside of a cat, we can do a replace inside of substring.
So that's enough of replace.
We're going to continue with a couple more and then we'll do some challenges.