PowerShell code to split a space delimited string – with double spaces

I am working on a PowerShell wrapper script to run multiple commands on multiple machines. The first thing I wanted to do was to chop up a space or comma delimited argument. (If you make an argument mandatory, and the user does not provide it, PowerShell will prompt you for it – and when prompted you can use space delimited values).

I was going to use the split() function, but I did not know how to handle “multiple spaces”. For example:

("123  4 5678").Split() = 
123
 
 
4

5678"

I would end up with “empty” array members. I found this post, and they guy used “| where-object {$_ -ne ”“}” to filter out the empty values. But then I stumbled upon a better way to do it, using the built in “StringSplitOptions” of  RemoveEmptyEntries.

("123  4 5678").split(" ",[StringSplitOptions]'RemoveEmptyEntries')

would result in:

123
4
5678

If you don’t know if you are going to have space or comma delimited then I used:

$Argument.split(" ",[StringSplitOptions]'RemoveEmptyEntries') | foreach {
$_.split(",",[StringSplitOptions]'RemoveEmptyEntries')}

5 Responses to PowerShell code to split a space delimited string – with double spaces

  1. Stephen Mills November 18, 2011 at 7:09 pm #

    You can also use the split operator. This allows you to use regular expressions in the matching. In the following example you are splitting on one or more space characters. It will split multiple spaces as a single split which gets rid of the blanks.
    “123 4 5678” -split ‘ +’

  2. Stephen Mills November 18, 2011 at 7:21 pm #

    I guess I should actually address both parts of what you are doing and not just the first. To address the second script where you don’t know if it is space or comma delimited, then you could actually use the following where it matches on one or more commas or spaces and splits on that.
    “123 ,77,,,,, 4 5678” -split ‘[ ,]+’
    123
    77
    4
    5678

  3. jbmurphy November 21, 2011 at 9:05 am #

    Thank you for taking the time to comment! I have to admit my regex skills are pretty weak, so I usually shy away from any solutions that use them. Can you think of any reason why the method I wrote about would not work? Or where your method has advantages? Thanks.

  4. Steve April 2, 2012 at 5:34 pm #

    Stephen’s solution will handle your problem very cleanly. If you are creating a function, I assume you looked at $args and it does not have what you need.
    I was looking for a solution that split on the double spaces *only* while keeping single spaces in each result which is why the “double spaces” in the title caught my eye. This article helped me realize I should handle commas as well for my problem.
    I need to split an address field on double spaces (or commas) when it is too long. This is what I came up with.
    ‘8 HILLSIDE VILLA CT LOT 8, Box 873’ -split ‘[ ]{2}|,’,2
    or
    ‘8 HILLSIDE VILLA CT LOT 8 Box 873’ -split ‘[ ]{2}|,’,2
    This splits on two or more spaces (the {2} tells it that) or a comma. The last 2 says I only want two result strings.
    I can assign the result to a variable like $a and use each piece as $a[0] and $a[1].

    Steve

Trackbacks/Pingbacks

  1. How to split string to an array of strings without multiple spaces - BlogoSfera - August 1, 2015

    […] 2: In this blog and on this question they suggested to use [System.StringSplitOptions]::RemoveEmptyEntrie + […]