strings_splitjoin.go 803 B

123456789101112131415161718192021222324252627282930313233
  1. // strings.go
  2. package main
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. func main() {
  8. str := "The quick brown fox jumps over the lazy dog"
  9. sl := strings.Fields(str)
  10. fmt.Printf("Splitted in slice: %v\n", sl)
  11. for _, val := range sl {
  12. fmt.Printf("%s - ", val)
  13. }
  14. fmt.Println()
  15. str2 := "GO1|The ABC of Go|25"
  16. sl2 := strings.Split(str2, "|")
  17. fmt.Printf("Splitted in slice: %v\n", sl2)
  18. for _, val := range sl2 {
  19. fmt.Printf("%s - ", val)
  20. }
  21. fmt.Println()
  22. str3 := strings.Join(sl2,";")
  23. fmt.Printf("sl2 joined by ;: %s\n", str3)
  24. }
  25. /* Output:
  26. Splitted in slice: [The quick brown fox jumps over the lazy dog]
  27. The - quick - brown - fox - jumps - over - the - lazy - dog -
  28. Splitted in slice: [GO1 The ABC of Go 25]
  29. GO1 - The ABC of Go - 25 -
  30. sl2 joined by ;: GO1;The ABC of Go;25
  31. */