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 <[email protected]> wrote:

> Sorry, but why is that?
>
> On Tuesday, June 26, 2012 at 6:27:14 AM UTC-7, Peter S 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
>>
>> On Tue, Jun 26, 2012 at 10:07 PM, Googol Lee <[email protected]> wrote:
>>
>>> 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.
>>>
>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/19bfe1af-bf5f-4c54-9198-3d23f9bec14a%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/19bfe1af-bf5f-4c54-9198-3d23f9bec14a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD9_o2pWyzgy-i_PpOpDO-RbbGN4KuJ7ZkHqsY309ot0pA%40mail.gmail.com.

Reply via email to