golang split string code example

Example 1: golang string split

s := strings.Split("a,b,c", ",")

Example 2: golang substring

s = s[:len(s)-1]

Example 3: golang split spaces

someString := "one    two   three four "

words := strings.Fields(someString)

fmt.Println(words, len(words)) // [one two three four] 4

Example 4: subdevide string golang

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.Split("a,b,c", ","))
	fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
	fmt.Printf("%q\n", strings.Split(" xyz ", ""))
	fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
}

Tags:

Misc Example