Menu Close

Go – SplitAfter() function in strings package in go

Here, We will learn about SplitAfter() function in strings package in go.  SplitAfter() function is used to slices string into all substrings after each instance of separator  in go golang.

Function prototype:

func SplitAfter(str, sep string) []string

Return value:

SplitAfter() function in strings package returns
slices string into all substrings after each
instance of separator and returns a slice of those substrings.
If string does not contain separator and separator
is not empty, then SplitAfter() returns same string.

Example with code:


package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
 
  s := strings.SplitAfter("a,b,c", ",")
  fmt.Println(s)
 
  s = strings.SplitAfter("Hello Go Golang", "o")
  fmt.Println(s)
 
  s = strings.SplitAfter("abc", "")
  fmt.Println(s)
 
  s = strings.SplitAfter("a,b,c", ",")
  fmt.Println(s)
 
  s = strings.SplitAfter("xyz", ",")
  fmt.Println(s)
 
  s = strings.SplitAfter("xyz", "p")
  fmt.Println(s)
 
 
}

Output:

[a, b, c]
[Hello Go Go lang]
[a b c]
[a, b, c]
[xyz]
[xyz]

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/
Posted in golang, strings

Leave a Reply

Your email address will not be published. Required fields are marked *