Skip to content

string.Split doesn't work with multiple separator characters #13726

@jjanuszkiewicz

Description

@jjanuszkiewicz

Steps to reproduce

"1x2y3".Split(@('x', 'y'))

Expected behavior

1
2
3

Actual behavior

1x2y3

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Comments

Works correctly in Windows Powershell 5.1.19041.1 (the same command returns the result described in Expected behavior).
Respective method in .NET Core 3.1 also splits the string correctly:

foreach (var item in "1x2y3".Split(new[] { 'x', 'y' }))
    Console.WriteLine(item);

output:

1
2
3

A single separator works correctly in PS 7.0.3:

"1x2y3".Split(@('x'))

output:

1
2y3

Also in this syntax:

"1x2y3".Split('x')

output:

1
2y3

I'm not sure if this is also a bug (and in which PS version), but there is one more difference between PS 5 and PS 7: PS 5 seems to treat "xy" as an array of two separators while PS 7 seems to treat it as one 2-character separator (PS 7 behavior seems more correct here):

PS 5:

"1x2y3xy4".Split("xy")

output:

1
2
3

4

PS 7:

"1x2y3xy4".Split("xy")

output:

1x2y3
4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions