Menu Close

Go – Split() function in strings package in go

Here, we are going to learn about Split() function in strings package in go golang. Split() function slices string into all substrings separated by separator. It returns a slice of the substrings between those separators.

Function proto type:

 func Split(str string, sep string) []string 

Return value:

In the Split() function proto type, If str does not contain sep and sep is not empty, Split() function returns a slice of length 1 whose only element is str.

If sep is empty, Split() function splits after each UTF-8 sequence. If both str and sep are empty, Split() function returns an empty slice.

Example:

package main

import (
	"fmt"
	"strings"
)

func main() {

	fmt.Printf("%q\n", strings.Split("x,y,z", ","))
	
	fmt.Printf("%q\n", strings.Split("Hello Hii techieindoor", "i "))
	
	fmt.Printf("%q\n", strings.Split(" pqr", ""))
	
	fmt.Printf("%q\n", strings.Split("", "Hello O'Phiphi"))

        fmt.Printf("%q\n", strings.Split("hello", ""))
	
}

Output:

["x" "y" "z"]
["Hello Hi" "techieindoor"]
[" " "p" "q" "r"]
[""]
["h" "e" "l" "l" "o"]

To learn more about golang, Please refer given below link:

 https://www.techieindoor.com/go-lang-tutorial/ 

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
Posted in golang, packages, strings

Leave a Reply

Your email address will not be published.

Contact Us