Now, let's look at working with arrays and splitting and
joining them. So I'm going to create a string.
Okay, and just Roy Biv. I can split that string into an array.
All strings have a split method, and this will split on whatever
whitespace is in the string, and then I get an array.
In this case, I get the first name and last name in the array.
However, you could also create a string like this where I've got
"This.Is.A.String" separated by periods. I can split this as
well. In this case, I want to specify, and this is the delimiter
that I'm splitting on.
There are some other kind of fun things that you can do with this split method.
Create another string here. However, I don't want to split on all of the spaces.
I'm going to split on a space, but I only want three elements to the array.
So once PowerShell gets to the third element or last
element, then it will stop splitting.
So I get PowerShell, split, 7, split, and then that's a
count of 3, and so it doesn't split anymore.
However,
that split method is kind of limited because you can only split on a single
delimiter. There's an advanced way to do this with a split operator. So I'm
going to create a string, and this could be the type of string that you might
get from a log file of something or some process that runs and creates a text
file, and you want to parse the data.
So I've got some data string that is separated by numbers, 44, 88, and 435.
And I want to pull that data out using those delimiters. Here's how
I'm going to do that. I'm going to split $r using the split
operator, and I'm going to use a regular expression pattern. Again,
and this is a little advanced.
We haven't talked about regular expressions.
If you understand regular expressions, then this will make sense to
you, but I'm basically telling PowerShell split on a space at least
two digits and then another space.
So what's in $d? I've got four items, and there they are.
So I've got Data foo bar, that first element, then I have a number, or my
delimiter of the space, number, space, and then Field1, and so on.
So that's how I was able to split that string using the split
operator and that regular expression pattern.
If you have things that are a little more complicated,
this is what you're going to need to do.
But that first element, $d[0], could also be treated as an array.
I can split that as well and get those values individually if I needed to.
There's an about topic, again,
you should take a look at. You kind of getting the feeling
I'm urging you to read the help, and there's lots of help
there for you to read? Sure enough.
Alright, so now we need to join things together.
So I'm creating an array, 2, 4, 6, 8, and you can see the values in there.
There is a join operator, ‑join, and then you specify the variable.
Get a result of 2, 4, 6, 8. This looks a little weird, and it's kind
of one of the few operators where you do the operator first and then
the thing that you are working on.
So I'm telling PowerShell, take the array, $r, and join the elements together.
By default, it doesn't include any spaces or delimiters.
You can, however, specify using this syntax,
put the variable first,
then the operator, and then the delimiter that you want to use
such as a space, or maybe you want a dash, whichever you need.