Home » Golang Strings.SplitAfter() Function with Examples

Golang Strings.SplitAfter() Function with Examples

Golang strings.SplitAfter() function splits a given string into substrings after each instance of the separator, it returns the string array.

strings.SplitAfter() function is different than strings.Split() function.

SplitAfter() breaks the strings into substrings after each instance of the separator. However, the Split() function splits the string into substrings based on the separator.

Syntax

func SplitAfter(s, sep string) []string

Where,

s = given string

sep = is the delimiter

Output: Returns string array.

If s does not contain a sep and a sep is not empty, it will return the s string of length 1.

If sep is empty, SplitAfter() breaks the string s after each UTF-8 sequence

if string s and sep are empty, it will return an empty slice.

In this article, we will discuss how to use strings.SplitAfter() function in golang to split a string into substrings after each instance of a separator.

How to Split String by strings.SplitAfter() in Golang

To split a string using strings.SplitAfter() function in Golang, pass the two arguments to the function SplitAfter(), a string and a separator. This function returns a string array.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Declare the string
	// call the SplitAfter function to split the string
	// separator is empty, hence splitAfter splits the string after each character

	s1 := strings.SplitAfter("Hello World!", "")
	fmt.Println(s1)
	fmt.Println("Length of th slice is:", len(s1))

}

In the above golang program, it imports strings package to use the SplitAfter() function and fmt package.

Golang strings SplitAfter() function takes 2 arguments:

  1. string “Hello World!”
  2. the separator is empty

The strings.SplitAfter() function splits the string Hello World! after each UTF-8 sequence and returns a slice of those substrings.

The output of the above golang program to split a string using the SplitAfter() function is:

[H e l l o   W o r l d !]
Length of th slice is: 12

Cool Tip: How to use strings.Split() function in Golang!

Golang SplitAfter() Function Examples

Let’s understand the Golang SplitAfter function with more examples.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Declare the string
	// call the SplitAfter function to split the string
	// separator is !, splitAfter splits the string after !
	// It splits without removing delimiter

	s1 := strings.SplitAfter("Hello! Welcome to GolangSpot", "!")
	fmt.Println(s1)
	fmt.Println("Length of th slice is:", len(s1))

}

In the above golang program,

To split a string without removing the delimiter use a SplitAfter(), follow the below steps

  1. Declare a string “Hello! Welcome to GolangSpot
  2. Separator as !
  3. Use the SplitAfter function that takes s1 and sep
  4. Store the slice of the substrings in s1
  5. Use fmt.Println to print s1 and its length

After splitting the string by a delimiter using the SplitAfter function, it splits the string without removing the delimiter.

The output of the above Golang SplitAfter function to split a string into substrings is:

[Hello!  Welcome to GolangSpot]
Length of th slice is: 2

Cool Tip: How to use Split() to tokenize string in Golang!

Conclusion

I hope the above article on how to split a string using the SplitAfter function in golang is helpful to you.

The strings.SplitAfter() function breaks the string into substrings after each instance of a separator without removing the delimiter from the output string.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment