Skip to content

strings: Split returns an array of length one for an empty string with non-empty separator #54455

Description

@roskee

What version of Go are you using (go version)?

1.19

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go playground

What did you do?

I tried to split an empty string with a non-empty separator using strings.Split method.

values := strings.Split("", ",")
fmt.Println(len(values))

The result was an array of length one with a value of an empty string at index 0. But if the separator is empty the result becomes an array of length 0. But the separator shouldn't have made a difference on the result since the string I am trying to split is empty in the first place.
here is the link to my program.
playground link

What did you expect to see?

The output should have been 0 since the strings I wanted to split is empty in the first place.

What did you see instead?

It resulted in an array of length 1. which has an empty string at index 0.

Activity

  1. roskee commented on Aug 15, 2022

    @roskee
    Author

    Apparently, this is how it was planned to behave.

  2. reopened this on Aug 15, 2022
  3. seankhliao commented on Aug 15, 2022

    @seankhliao
    Member

    Working as documented:

    If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

    If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

  4. locked and limited conversation to collaborators on Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions