Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
roskee opened this issue Aug 15, 2022 · 2 comments

Comments

@roskee
Copy link

roskee commented Aug 15, 2022

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.

@roskee
Copy link
Author

roskee commented Aug 15, 2022

Apparently, this is how it was planned to behave.

@roskee roskee closed this as completed Aug 15, 2022
@roskee roskee reopened this Aug 15, 2022
@seankhliao
Copy link
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.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2022
@golang golang locked and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants