Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Split on whitespaces

  • Fields
  • Split
package main

import (
	"fmt"
	"strings"
)

func main() {
	expression := "  hello    space   world 42 "
	fmt.Printf("'%s'\n", expression)
	parts := strings.Fields(expression)
	fmt.Println(parts)
	fmt.Println(len(parts))
	fmt.Println()

	for _, part := range parts {
		fmt.Printf("%v\n", part)
	}
}
'  hello    space   world 42 '
[hello space world 42]
4

hello
space
world
42
'  hello    space   world 42 '
[hello space world 42]
4