Powershell: (PSCore, Linux) Split method does not work with multiple delimiters

Created on 21 Aug 2018  路  10Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

$a = "192.168.2.1/25"
$a.Split("./")

Expected behavior

192
168
2
1
25

Actual behavior

192.168.2.1/25

Environment data

> $PSVersionTable
Name                           Value                                                                                         
----                           -----                                                                                         
PSVersion                      6.0.4                                                                                         
PSEdition                      Core                                                                                          
GitCommitId                    v6.0.4                                                                                        
OS                             Linux 4.14.65-1-MANJARO #1 SMP PREEMPT Sat Aug 18 13:29:56 UTC 2018                           
Platform                       Unix                                                                                          
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                       
PSRemotingProtocolVersion      2.3                                                                                           
SerializationVersion           1.1.0.1                                                                                       
WSManStackVersion              3.0     

Comments

PowerShell Core 6.0.4 installed on Manjaro Linux (Arch) via snapcraft

Performing a split operation with one delimiter, i.e.
> $a.Split('.')
still provides expected output:
> 192
> 168
> 2
> 1/25
Issue-Question Resolution-Answered Resolution-Duplicate

Most helpful comment

It is not a bug, but an unfortunate - and unavoidable - consequence of a new method overload that is [string]-typed having been introduced in .NET _Core_.

This issue is therefore a duplicate of #3809; specifically:

The simplest solution is to cast to [char[]] or use .ToCharArray():

 "192.168.2.1/25".Split([char[]] "./")

All 10 comments

Not sure what's the issue here... I'm using WSL Linux Ubuntu, and in PowerShell Core.

Are you trying to use the split method with two characters: "." and "/"? The following line work as expected.

PS /home/maxt> ($a.Split('.')).split('/')
192
168
2
1
25
PS /home/maxt>

wsl_split_01_2018-08-21_12-56-11

I don't think the split method allows multiple characters.

Hi Max,
I'm trying to use multiple delimiters, as described in online resources. $a.Split('./')

Tested and working in Windows PowerShell 5.1.
(I'm not in close proximity of a personal windows machine at the moment, will try to get you a screenshot...)

Ah! Found some docs: https://docs.microsoft.com/en-us/dotnet/api/system.string.split?redirectedfrom=MSDN&view=netframework-4.7.2#overloads

It states that its possible to create an array of characters and then use the split() method. But I haven't figured it out how in PowerShell.

The following should work but it doesn't:

PS /home/maxt> [string[]] $sep = (".","/")
PS /home/maxt> $sep
.
/
PS /home/maxt> $a.Split($sep)
192.168.2.1/25
PS /home/maxt>

I got use to use the dual split().

:)

Good Catch!!
Ah! You didn't mention it work in Windows PowerShell. It's a bug!
:)

Either methods works in Windows PowerShell.
I'm sure it will be fix!

winps_split_01_2018-08-21_12-56-11

Is this what you want
PS> $a = "192.168.2.1/25"
PS> $a.split([char]".",[char]"/")
192
168
2
1
25

Tested and Works with v5.1, v6.0.4 and v6.1.0-preview4

Either works... But yes, using more than one delimirer in a Split command, e.g. Split('/ :;$.,') works for me in Windows PowerShell

It is not a bug, but an unfortunate - and unavoidable - consequence of a new method overload that is [string]-typed having been introduced in .NET _Core_.

This issue is therefore a duplicate of #3809; specifically:

The simplest solution is to cast to [char[]] or use .ToCharArray():

 "192.168.2.1/25".Split([char[]] "./")

Thanks @mklement0!

Leave a comment

Thanks everyone!

Was this page helpful?
0 / 5 - 0 ratings