So let's go back into the PowerShell console,
we'll look at how we manipulate string data by first replacing string values,
then we'll split string values,
and then we'll actually pad some string values as well.
And next thing to look at is using things like replace and split so that
we can take a string value and do something with it.
So let's just create a new variable1,
and what I'm going to do first is just null the values out just to make sure
that any of the previous values that we had have been removed,
and then what I'll do is I'll say,
THe class instructor asked for a volunteer for a demonstration.
Okay, that's my first variable.
Then for my second variable, I'm going to say Jones and Tom, so two values.
Okay, so what can we do with that?
Well, firstly, what we can do is we can look at using replace,
so I'm going to use my variable values that I just created,
so variable1, and the sentence was,
THe class instructor asked for a volunteer for a demonstration.
But actually, what I realized is it shouldn't say instructor.
It should say teacher, so I'm going to say "instructor", "teacher".
So what we're doing here is I'm saying,
get the value from variable1 then replace whatever
you see with the word teacher.
So I'm going to go over here, and you'll see,
THe class teacher asked for a volunteer for a demonstration.
Now, if I go back into variable1,
you'll see it still says the class instructor because
we didn't actually change the value.
We just replaced it as it was coming back.
Now, of course,
that's actually a common technique that's often used that what you often
have is a static value of some description that you may want to modify
and change as you go up and down the script,
but ultimately, you don't want to change the original value.
So what we can do here is call a new variable.
Let's say we call it replacevariable.
I can then say variable1.
I could repeat the same process, so replace "instructor", "teacher".
And so now, if I do that and say replacevariable,
that has the new value, whereas the variable1 I had still has the original value,
but it means that I can always call back to that original one if
I ever need to get those values to come back.
Now,
another option that we've got is the ability for kind of moving things around,
so we can use regex replace to kind of move things around.
So if we look at our variable2, for example, it says, Jones Tom.
I actually just realized that it should be Tom Jones.
So how do we move that around?
Well, what we can do is we can say variable2,
and we can use a replace, which is the standard function,
and then now we can use some combination of regex to be able to do this.
Now, of course, as you know,
regex is normally a combination of certain types of brackets with
letters to kind of identify where things are.
So what we're doing here is we're going to take the first one,
and we're going to split into the second one.
So if I just do this here, a‑z, and then do a plus on this side,
so basically, it's going to look for the space in the middle,
take the two values.
Once I have that split then I end up with two kind of variables, one is a $2,
and one is $1, which basically is the output of it being chopped up.
So you can see that we took variable to,
which was Jones and Tom, used regex to find the first word,
the second word, put them into various containers, so $1,
$2, and then swap them around, so what you end up with now is Tom,
Jones with a comma because I added the comma into the middle.
Now, if I didn't add the comma, that could just be a space,
and it would just say, Tom Jones.
So we can use the replace to not actually replace words or text values,
but actually to move things around and swap them as we need it.
We can actually go even simpler at this point.
So if I get rid of all of this extra piece here,
if I just go back to replace,
and what I can do is simply use just some quotes here,
put the brackets, use a chevron, a‑z, and then I can do that,
and that will literally just remove spaces.
So the replace function serves a wider purpose than just replacing text values.
So just be aware that it can be joined with other pieces too.
Now, the other option that we have is to use the split.
Now, I love using the split because often,
when we have large amounts of information,
we want to split it into smaller kind of pieces so
that we can run something over it.
So if I just type the word split, for example,
Jan Feb Mar Apr May Jun,
so I have a series of values that are just spaced out in that text value.
If I do split, it'll chop them each into a specific line.
So that's one way of doing it.
We may get a string set value that comes back, like this.
Now,
I'm pretty sure that most of the data that you will get back
from various things will be comma separated.
So in this instance, we can change the syntax around.
So we're going to take the value first, so this is my,
effectively, my array of data.
I can then say, I want to split that information,
and then I'm going to say, I want to use a comma for that and do Enter,
and it will do the same thing.
So the split can either use default spaces because the
default delimiter is just a space, or we can tell it what the delimiter might be.
Now, we can even go even crazier than this.
What we can do is if we bring that back here,
I've got the split with the comma. I could then go a little bit further and say,
only get me three values.
Now, notice what happens.
It splits three times and then concatenate or joins the values together,
so I get separate Jan, separate Feb, and then a Mar,
April, May,
Jun all together because I asked it to give me three values out of it.
So let's try that one again.
But this time, what we'll do is we'll, instead of using it all on one line,
we'll actually have our variable.
So we've got our variable, I'm going to call it variablesplit,
and we'll do the same thing as before, Jan,
Feb, Mar, Apr, Maty, Jun, like so,
so I have a variable called variablesplit, let me,
variablesplit, and I give it my values.
I couldn't spell May properly, but that'll do for now.
So we have our values.
What we can then do is because we put them into a variable,
when I do variable and call the name, I can then use Split.
It becomes a method of the object.
So I can go in here and say, I want to split it on the comma and sure enough,
I get the same again.
So if we're using data that's been injected into a variable,
then of course we can come and split that value as we need to.
Now, what about if the values that you're using,
so this is where I get to correct my Maty,
let's say we have a combination of the separators.
So instead of the data being nice and clean and it comes back as commas,
maybe we have a mix of commas, and semicolons,
and something else,
so I'm going to make sure my variable is populated with those values,
and I'll just make sure the values come back.
Sure enough, you can see the mix there, commas and the semicolons.
What I do have the ability to do, I'll tell you what,
let me just clear this so we can see it,
is I can go to a variablesplit, and I can use the Split method again,
and I'm going to say, do that with a comma,
then I'm going to say Split again and use the second
separator that we have, and when I do Enter,
they all split nicely.
So we have the flexibility of being able to split the data even
if it comes back in a strange way with multiple characters or
delimiters that we can utilize.
Now, of course, the last one that we can utilize here is called padding,
so the ability for us to add values or padding of some description
to another text value of something or a number.
This is done normally, let's say, you're passing a number around,
and maybe all employee numbers have a series of 0s before it,
you could pad all of those.
So I'm going to call my variable pad1,
and I'm just going to write the word Demonstration.
I am also then going to create a new one,
pad2, and it will also have the word Demonstration,
just text values.
Now, what we can do here is we're going to focus on pad1.
So pad1, what we can do is go to the variable,
and then you'll see we have an option called PadLeft,
which is a function or method associated to that value.
I can then say 14, and I'll explain what 14 is,
and then I'll just say, Added, like so.
Now, notice what happened here.
It says, cannot convert the paddingChar,
and that's good because you can't add a word to the end of it.
What you have to do is add a character.
And what you'll see is when I did that, it added A to the beginning of that.
If I was to repeat that, and let's change this a little bit,
and say, PadRight,
but we'll do that on number 2, and, let's say, we change that to
B, you'll now get the B at the other end of it.
So when you're padding characters,
you need to determine what the character is that you want to kind of
pad and then what you're trying to pad into because they are just
effectively string methods that are in .NET itself. So it can be quite
useful to kind of pad something, if you needed to,
to add something to the end of it.
But when you combine all of these together where you can replace,
you can then split and you can pad, you're able to manipulate the string values in the best way possible.