Using the Split Method in PowerShell

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Split method in Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Dude, dude, dude, (or dudette, as the case may be) I still cannot find my teapot after our move. At least I found my kettle to heat water so I can make tea, but unfortunately, without a teapot, I am stuck drinking “bagged” tea leaves. It is not a major crisis yet, but I do miss blending my own teas with my own herbs. And of course, my various tins of teas and herbs are sitting here, just waiting for me to locate the teapot. On the bright side, I did pick up some nice tea bags when the Scripting Wife and I were in Europe, so it is not a total loss. In fact, I got a rather nice oolong tea in little triangle sachets that is actually not bad.

Dude, let's split

The Split method from the System.String class is not a static method. This means that when I have a string, I can gain access to the Split method. But if I do not have a string, I cannot access it. The MSDN documentation for the String.Split Method lists six overloads for this method. So I have a lot of flexibility on how I might want to use the method. I will not discuss all six overloads today, but I will discuss the three main ways of using the method:

  • Split on an array of Unicode characters
  • Split on an array of strings with options
  • Specify the number of elements to return

The additional ways of calling the method will behave in a similar fashion.

Split on an array of Unicode characters

One of the cool things about the Split method is that it will accept an array of things upon which to split. Therefore, I can split on one or more Unicode characters.

Note  A handy list of Unicode characters and their associated code values appears on Wikipedia. When using them in Windows PowerShell, remove the U+ and change it to 0X, then call it with [Char]. For example, the right curly brace is [char]0X007D.

The first thing I need is a string with Unicode characters embedded inside it. So here is my string:

$string = "a powershell $([char]0x007B) string $([char]0x007D) contains stuff"

Now I need to create an array of two characters to use to split the string. Here is what I come up with the first time:

$string.Split("{,}")

And this command works. Here is a screenshot of the string, my split characters, and the associated output:

Image of command output

What is cool is that I can do this same thing in reverse. Here is an example of reversing the process:

PS C:\> $string2 = "a powershell {string} contains stuff"

PS C:\> $string2.Split([char]0x007B, [char]0x007D)

a powershell

string

 contains stuff

PS C:\>

Split on an array of strings with options

The option to specify an array of strings to use for splitting a string offers a lot of possibilities. The StringSplitOptions enumeration also offers a way to control the return of empty elements. The first thing I need to do is to create a string. Here is my string:

$string = "This string is cool. Very cool. It also rocks. Very cool. I mean dude.Very cool. Very cool."

Now, I need to specify a separator. To do this, I will assign an array of strings to the $separator variable as shown here:

$separator = "Very cool.","."

Now, I need to create my StringSplitOption. There are two options: None and RemoveEmptyEntries. The first time I run the command, I will remove the empty entries. So here is that command:

$option = [System.StringSplitOptions]::RemoveEmptyEntries

The command and the output from the command appears here:

Image of command output

When I change the option to None, I see that there is one extra space at the end of the line. This happens because the words “Very Cool.” appear twice at the end of the string. This is shown here:

Image of command output

Specify the number of elements to return

It might be useful to return only a certain number of elements from a string. Here is an example using a string array as a separator:

$string = "This string is cool. Very cool. It also rocks. Very cool. I mean dude. Very cool. Very cool."

$separator = "Very cool.","."

$option = [System.StringSplitOptions]::RemoveEmptyEntries

$string.Split($separator,2, $option)

The first time I ran the command, it generated an error message. The reason is that I added the number of elements to return to the end of the method call. The last position is for the Split option. Here are the commands and the associated output:

Image of command output

That is all there is to using the Split method. String Week will continue tomorrow when I will talk about more string stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Nikhil Vasantham 0

    hi
    this is the format to be splitted
    edituser (‘*****’) comment(‘*****’) admin owner(‘*****’) audit(*) interval(*) (i.e., every line consists of this format)
    and the data be like
    note:the value of the editusr starting with “_” and if the value is “system” that line data not to be picked up
    editusr (“_undefined”) comment(‘????????????????????????????’) logical audit- editusr (“aiba kazuo”) owner(‘SYSTEM’) audit(FAILURE LOGINSUCCESS LOGINFAILURE) editusr (“cba_anonymous”) comment(‘/S/999999//’) owner(‘SYSTEM’) name(‘cba_anonymous’) audit(FAILURE LOGINSUCCESS LOGINFAILURE) editusr (“E131687”) comment(‘JPM/I/131687/IBM/ISHIDA.ATSUKO’) owner(‘SYSTEM’) audit(FAILURE LOGINSUCCESS LOGINFAILURE)
    editusr (“SYSTEM”) owner(‘SYSTEM’) audit(FAILURE LOGINSUCCESS LOGINFAILURE)

    please help me with this.
    thanks in advance

Feedback usabilla icon