how are these different -split

This topic contains 8 replies, has 5 voices, and was last updated by Profile photo of Tim Curwick Tim Curwick 1 year, 10 months ago.

  • Author
    Posts
  • #29200
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    I am trying to sort out the basics. What is the difference between using split in these two different ways?

    PS C:\> $a.split("he").length
    57
    PS C:\> ($a -split "he").Length
    5
    PS C:\>
    
  • #29201
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    $a.split("he").length
    splits at each individual "h" and "e" character

    whereas

    ($a -split "he").Length
    is splitting when both "he" characters are present together.

  • #29202
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    In a more general sense why is .split() different than -split? I thought they were the same just in a different format.

  • #29203
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    It's also notable to mention that .split() is a method of the system.string class:

    https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx

    and -split is a powershell console operator

    https://technet.microsoft.com/en-us/library/hh847811.aspx

    or do Get-Help about_split

    Looking at the MSDN documentation for the split method for system.string objects you can see that the only constructor for the split method with one argument accepts a char array or "char[]". Since you've supplied "he" as the argument, the operator will convert that to a char array or char[] ("h","e") and split on the individual letters, which explains the behaviour described by Curtis.

    To mimic the default behaviour of -split with the .split() method, you can do the following:

    $a.split(@("he"),[system.stringsplitoptions]::RemoveEmptyEntries)

  • #29204
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    Thanks. That clears up a lot 🙂

  • #29205
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    The -split operator is essentially wrapping the [regex]::Split() method, rather than string.Split().

  • #29217
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Peter,

    Adding [StringSplitOptions]::RemoveEmptyEntries to .Split() does not make it act like -Split in most cases, as .Split() will still split on each individual character.

    For example, ("weather") -Split "he"
    results in
    weat
    r

    but ("weather").Split( "he", [StringSplitOptions]::RemoveEmptyEntries )
    results in
    w
    at
    r

  • #29218
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Tim, the key to Peter's example is the @() around the split criteria. I believe [StringSplitOptions]::RemoveEmptyEntries just puts it into the overload scenario that is needed for the criteria to be recognized as a string. To be honest, I am not a programmer by trade, so I'm just doing my best here to understand what is going on. The way I read this, Split is going to break down the split criteria, if you pass it a string, it breaks it down into characters, if you pass it an array of strings, it uses the whole string.

    In your example
    ("weather").Split( @("he"), [StringSplitOptions]::RemoveEmptyEntries )

    Results in:
    weat
    r

    Another example
    ("weather teather").Split( @("he", "ea"), [StringSplitOptions]::RemoveEmptyEntries )

    Results in
    w
    t
    r t
    t
    r

  • #29223
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Ah, I see. I was focusing on RemoveEmptyEntries and why that wouldn't help. But it's not really there to remove the empty entries, it's just there to get .Split() to use an overload that can take an array of strings instead of an array of characters. And then the @() tells it you want it to be an array with a single string, instead of a string representing an array of characters.

    Nice.

    This would then do the same thing, with less typing (and without removing empty entries, which you usually don't want to do anyway.

    ("weather").Split( @("he"), 'None' )

You must be logged in to reply to this topic.