Loading, please wait...

A to Z Full Forms and Acronyms

How to Split a String in GoLang? | Go Programming Tutorial

Nov 20, 2022 #GoProgramming #GoLanguage, 804 Views
In this article, you will learn about how to split a string in Go Programming Language.

How to Split a String in GoLang? | Go Programming Tutorial

In this article, you will learn about how to split a string in Go Programming Language.

In Go Programming Language, Strings are different from other programming languages. In GoLang, it is a sequence of variable-width characters where every character is indicative by one or more bytes using UTF-8 encoding. In simple words, strings are the immutable chain of arbitrary bytes which also includes bytes with zero values.

These functions are defined in the string package, so the user has to import the strings package in your program to access these functions:

  • Split: The function splits a string into all substrings separated by the separator and returns a slice that contains these substrings.

Syntax:

func Split(str, sep string) [ ] string

Here, str is the current string and sep is the separator. If str does not have the given sep and sep is not empty, then it will return a slice of length 1 which carries only str. If sep is empty, then it will split after each UTF-8 sequence. The other case is if both str and sep are empty, then it will return an empty slice.

Example:

import (
    "fmt"
    "strings"
)
 
// Main function
func main() {
 
    // Create and initialize the strings
    str_1 := "Tutorials Link"
    str_2 := "We write about new technologies."
    str_3 := "We love to share."
 
    // Displaying strings
    fmt.Println("String 1: ", str_1)
    fmt.Println("String 2: ", str_2)
    fmt.Println("String 3: ", str_3)
 
    // Using Split() function
    res1 := strings.Split(str_1, ",")
    res2 := strings.Split(str_2, "")
    res3 := strings.Split(str_3, "!")
    res4 := strings.Split("", "Link")
 
    // Displaying the result
 
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)
 

Output:

String 1: Tutorials Link

String 2: We write about new technologies

String 3: We love to share.

Result 1: [Tutorials Link]

Result 2: [W e   w r i t e   a b o u t   n e w   t e c h n o l o g i e s]

Result 3: [We love to share]

Result 4: [ ]

  • SplitAfter: The function splits a string into all substrings after each instance of the current separator and returns a slice that has these substrings.

Syntax: 

func SplitAfter(str, sep string) [ ] string

Here, str is the current string and sep is a separator. If str does not have the sep and sep is not empty, then it will return a slice of length 1 which has only str. If sep is empty, then it will split after each UTF-8 sequence. The other case is if both str and sep are empty, then it will return an empty slice.

  • SplitAfterN: The function splits a string into all the substrings after each instance of the given separator and returns a slice that has these substrings.

Syntax:

func SplitAfterN(str, sep string, m int) [ ] string

Here, str is the current string, sep is the separator, and m is used to find the number of substrings. If m>0, then it returns at most m substrings and the last substring will not divide or split. If m==0, then it will return nil. If m>0, then it will give all the substrings in the output.

A to Z Full Forms and Acronyms