The next Pilton string function that we'll work with is called replace and what it does is it replaces
parts of a string.
So if you want 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 want to get rid of that we could use Replace hello world 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 hell only occurs once.
So that's what our output looks like.
Percent star Octa Thorpe.
Oh what's this thing called.
There's an actual name for the at symbol but there's a real name and then O world.
So if I copy that over and just run it now.
See that's the output that we get.
We could also do something like replace hello world and let's just replace all Elle's with sevens.
Why not.
And that's what we need to do there.
And then we get hello world but with sevens instead of ells you know we could do the same thing just
to show you one more with oh's and replace them with zero.
Now one thing that is important to pay attention to.
Let's say I have hello here with a capital O.
Hello.
Here with a lowercase Oh excuse me world with lower case so the idea just say have upper and lower case
oves in here.
If I try and replace 0 and then say I just want to replace it with.
That's 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 O here.
So that's one way to use your place the other way that I mentioned is that it can replace multiple things.
So here is a civic 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 wall space
and space.
Basically the output will be cheese and bread and coffee and milk inserted that right there.
Here here and here every time there's a space.
So if I tried 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's replace all E's with threes and we'll just do lowercase E's So anytime there's a lowercase like
namesake Norse American we'll replace it with three.
So to do that is a select replace and don't ask why.
It's not really a real world example but it's like a place you want to do title.
And then what are we trying to replace lowercase is what do we want to replace him 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 where you get the name sake
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 also you can combine
replace with other functions like we've already seen use concat and substring together so we could try
the same thing by using replace and Concat 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.
Highlighted it and on a Mac its command slash on a PC I believe it is control slash.
And when it commented out which means it just won't run it Tirso I can see it but sequel treats it are
minuscule 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 a
place 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 ease with
threes so that replace title the lower case e with three like that.
But I'm only going to take the first 10 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.
Now if I pasted in here you can see what it did was first replace always 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 appear 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 concat we can do a replace inside of substring.
So that's enough of a place we're going to continue with a couple more and then we'll do some challenges.