Split a empty string will get a one element array?

2,066 views
Skip to first unread message

Googol Lee

unread,
Jun 26, 2012, 9:07:54 AM6/26/12
to golan...@googlegroups.com
func main() {
  empty_string := ""
  result := strings.Split(empty_string, " ")
  fmt.Println(len(result)) // here length is 1
}

If split a empty string, and get a array with one empty string, is that right behavior? I think it should return a empty array.

Peter S

unread,
Jun 26, 2012, 9:27:14 AM6/26/12
to Googol Lee, golan...@googlegroups.com
It follows this simple rule: the length of the result slice is the number of occurrences of the separator plus one. It is the "right" (intended) behavior, not a bug; in fact godoc has an equivalent example: http://golang.org/pkg/strings/#Split

Peter

Googol Lee

unread,
Jun 26, 2012, 10:32:29 AM6/26/12
to golan...@googlegroups.com, Googol Lee
Got it. Thanks


On Tuesday, June 26, 2012 9:27:14 PM UTC+8, speter wrote:
It follows this simple rule: the length of the result slice is the number of occurrences of the separator plus one. It is the "right" (intended) behavior, not a bug; in fact godoc has an equivalent example: http://golang.org/pkg/strings/#Split

Peter

jac...@gmail.com

unread,
Aug 7, 2019, 6:14:18 PM8/7/19
to golang-nuts
Sorry, but why is that?

Ian Lance Taylor

unread,
Aug 7, 2019, 6:18:01 PM8/7/19
to jac...@gmail.com, golang-nuts
On Wed, Aug 7, 2019 at 3:14 PM <jac...@gmail.com> wrote:
>
> Sorry, but why is that?

Note that you are replying to a message that is more than seven years old.

That said: why is what? What are you asking?

Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/19bfe1af-bf5f-4c54-9198-3d23f9bec14a%40googlegroups.com.

Kurtis Rader

unread,
Aug 7, 2019, 6:43:05 PM8/7/19
to jac...@gmail.com, golang-nuts
Please don't respond to threads that are seven years old. Having said that the behavior is reasonable and the behavior you and the O.P. expect is not reasonable. Consider the following examples:

result := strings.Split("abc", "")
result := strings.Split("ab", "b")
result := strings.Split("", "")

The first statement yields a slice of three elements. The second a slice of two elements with the second being an empty string. What should the second statement yield? A slice of one element (the empty string) or an empty slice? The former is consistent with all the other cases while the latter is inconsistent.

On Wed, Aug 7, 2019 at 3:14 PM <jac...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/19bfe1af-bf5f-4c54-9198-3d23f9bec14a%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Marvin Renich

unread,
Aug 8, 2019, 6:36:25 AM8/8/19
to golang-nuts
* Kurtis Rader <kra...@skepticism.us> [190807 18:43]:
> Please don't respond to threads that are seven years old. Having said that
> the behavior is reasonable and the behavior you and the O.P. expect is not
> reasonable. Consider the following examples:
>
> result := strings.Split("abc", "")
> result := strings.Split("ab", "b")
> result := strings.Split("", "")
>
> The first statement yields a slice of three elements. The second a slice of
> two elements with the second being an empty string. What should the second
> statement yield? A slice of one element (the empty string) or an empty
> slice? The former is consistent with all the other cases while the latter
> is inconsistent.

You are mixing apples and oranges. The rule is different if the
separator is "" than if it is not empty. This playground link
https://play.golang.org/p/6nbTIW50i2g shows the difference. The OP was
asking about " " (non-empty) as the separator. The documentation for
Split is very specific about both these cases.

...Marvin

Reply all
Reply to author
Forward
0 new messages