×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strings.SplitAfter() Function with Examples

Golang | strings.SplitAfter() Function: Here, we are going to learn about the SplitAfter() function of the strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 19, 2021

strings.SplitAfter()

The SplitAfter() function is an inbuilt function of strings package which is used to split the given string (slice) into all substrings after each instance of the given separator. It accepts two parameters – a slice (string) and a separator and returns a slice of those substrings.

Note:

  • If the slice does not contain a separator and the separator is not empty, the SplitAfter() function returns a slice of length 1 whose only element is the given string.
  • If separator is empty, SplitAfter() function splits after each UTF-8 sequence. If both the string and separator are empty, SplitAfter() function returns an empty slice.

Syntax

func SplitAfter(str, sep string) []string

Parameters

  • str : The string (slice) whose value is to be separated.
  • sep : Separator.

Return Value

The return type of SplitAfter() function is a []string, it returns a slice of those substrings.

Example 1

// Golang program to demonstrate the
// example of strings.SplitAfter() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitAfter("The sun is very bright", " "))
	fmt.Printf("%q\n", strings.SplitAfter("a man a plan a canal panama", "a "))
	fmt.Printf("%q\n", strings.SplitAfter("The sun is very bright", ""))
	fmt.Printf("%q\n", strings.SplitAfter("", ","))
}

Output:

["The " "sun " "is " "very " "bright"]
["a " "man a " "plan a " "canal panama"]
["T" "h" "e" " " "s" "u" "n" " " "i" "s" " " "v" "e" "r" "y" " " "b" "r" "i" "g" "h" "t"]
[""]

Example 2

// Golang program to demonstrate the
// example of strings.SplitAfter() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%q\n", strings.SplitAfter("The#sun#is#very#bright", "#"))
	fmt.Printf("%q\n", strings.SplitAfter("The, sun, is, very, bright", ", "))
	fmt.Printf("%q\n", strings.SplitAfter("Delhi Mumai Indore Gwalior", " "))
}

Output:

["The#" "sun#" "is#" "very#" "bright"]
["The, " "sun, " "is, " "very, " "bright"]
["Delhi " "Mumai " "Indore " "Gwalior"]

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.