strings.Split behavior

89 views
Skip to first unread message

buc...@gmail.com

unread,
Feb 20, 2018, 3:50:49 PM2/20/18
to golang-nuts
package main

import (
  "fmt"
  "strings"
)

func main() }
  s := "this/that there/here that/this"
  tmp := strings.Split(s, "/")
  fmt.Println(tmp)
  for _, s1 := range tmp {
if strings.Contains(s1, "that") {
fmt.Println(s1)
}
  }
}

Output:
this was as expected:
[this that there here that this]

this was NOT expected:
that there
here that

By my reasoning, each loop through the range of tmp, s1 should have grabbed one word from the tmp slice and made the comparison with that word alone.  But instead the second fmt.Println() output implies that it grabbed more than one word.

I'm a noob and puzzled by this behavior.

Jan Mercl

unread,
Feb 20, 2018, 4:00:32 PM2/20/18
to buc...@gmail.com, golang-nuts
On Tue, Feb 20, 2018 at 9:51 PM <buc...@gmail.com> wrote:

> But instead the second fmt.Println() output implies that it grabbed more than one word.
>
> I'm a noob and puzzled by this behavior.

I don't know how the concept of word got involved in the "expected" behavior, but the code shown has nothing to do with something like "word". It works purely with strings and slices of strings and is AFAICT working as intended.


--

-j

Marvin Renich

unread,
Feb 20, 2018, 4:11:02 PM2/20/18
to golang-nuts
* buc...@gmail.com <buc...@gmail.com> [180220 15:51]:
> package main
>
> import (
> "fmt"
> "strings"
> )
>
> func main() {
> s := "this/that there/here that/this"
> tmp := strings.Split(s, "/")
> fmt.Println(tmp)
> for _, s1 := range tmp {
> if strings.Contains(s1, "that") {
> fmt.Println(s1)
> }
> }
> }

Look at https://play.golang.org/p/2tz2asuZcGc where I have changed

fmt.Println(tmp)
to
fmt.Printf("%#v\n", tmp)

and I think you will understand that tmp does not contain what you
thought it did. Split splits the string at "/", not at "/" and " ", so
tmp contains four strings, "this", "that there", "here that", and
"this".

...Marvin

buc...@gmail.com

unread,
Feb 20, 2018, 4:19:50 PM2/20/18
to golang-nuts


On Tuesday, February 20, 2018 at 2:00:32 PM UTC-7, Jan Mercl wrote:

I don't know how the concept of word got involved in the "expected" behavior, but the code shown has nothing to do with something like "word". It works purely with strings and slices of strings and is AFAICT working as intended.

Ah.  It just dawned on me what is going on.  Since the .Split function didn't encounter a '/' between the "that there" & "here that", both of those are subsequently evaluated as the s1 strings themselves.

If I had used a different fmt print command with a "\n" at the end of each s1 iteration, I'd have seen that.

Thank you, Jan!

buc...@gmail.com

unread,
Feb 20, 2018, 4:35:10 PM2/20/18
to golang-nuts

Thank you, Marvin.  About the same time you wrote this, the answer likewise dawned on me.

Krzysztof Kowalczyk

unread,
Feb 20, 2018, 8:52:16 PM2/20/18
to golang-nuts
Maybe this will help understand what Split() does:

https://play.golang.org/p/U7gkrBs8IaZ

func main() {
  s := "this/that there/here that/this"
  tmp := strings.Split(s, "/")
  fmt.Printf("%#v\n", tmp)
}

Output:
[]string{"this", "that there", "here that", "this"}

So when you loop over strings, it's expected the loop will pick up "that there" and "here that".
Reply all
Reply to author
Forward
0 new messages