How to Split a String in Golang?

In Go language, strings are unique in relation to different dialects like Java, C++, Python, and so forth It is a succession of variable-width characters where every single person is addressed by at least one bytes utilizing UTF-8 Encoding. In Go strings, you are permitted to part a string into a cut with the assistance of following capacities. These capacities are characterized under strings bundle thus, you need to import strings bundle in your program for getting to these capacities:

How to Split a String in Golang?

Split: This capacity parts a string into all substrings isolated by the given separator and returns a cut which contains these substrings.

Syntax:

func Split(str, sep string) []string

Here, str is the string and sep is the separator. If str doesn’t contain the given sep and sep is non-unfilled, then, at that point, it will return a cut of length 1 which contain just str. Or then again assuming the sep is vacant, then, at that point, it will part after each UTF-8 succession. Or then again on the off chance that both str and sep are vacant, it will return a vacant cut.

Example:

// Go program to illustrate how to split a string
package main
 
import (
    "fmt"
    "strings"
)
 
// Main function
func main() {
 
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
 
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
 
    // Splitting the given strings
    // Using Split() function
    res1 := strings.Split(str1, ",")
    res2 := strings.Split(str2, "")
    res3 := strings.Split(str3, "!")
    res4 := strings.Split("", "GeeksforGeeks, geeks")
 
    // 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:  Welcome, to the, online portal, of GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome  to the  online portal  of GeeksforGeeks]
Result 2:  [M y   d o g   n a m e   i s   N a w a b]
Result 3:  [I like to play Ludo]
Result 4:  []

SplitAfter: This capacity parts a string into all substrings after each occurrence of the given separator and returns a cut which contains these substrings.

Syntax:

func SplitAfter(str, sep string) []string

Here, str is the string and sep is the separator. On the off chance that str doesn’t contain the given sep and sep is non-unfilled, then, at that point, it will return a cut of length 1 which contain just str. Or then again on the off chance that the sep is unfilled, then, at that point, it will part after each UTF-8 succession. Or then again assuming both str and sep are vacant, it will return a vacant cut.

Example:

// Go program to illustrate how to split a string
package main
 
import (
    "fmt"
    "strings"
)
 
// Main function
func main() {
 
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
 
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
 
    // Splitting the given strings
    // Using SplitAfter() function
    res1 := strings.SplitAfter(str1, ",")
    res2 := strings.SplitAfter(str2, "")
    res3 := strings.SplitAfter(str3, "!")
    res4 := strings.SplitAfter("", "GeeksforGeeks, geeks")
 
    // 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:  Welcome, to the, online portal, of GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the,  online portal,  of GeeksforGeeks]
Result 2:  [M y   d o g   n a m e   i s   N a w a b]
Result 3:  [I like to play Ludo]
Result 4:  []

SplitAfterN: This capacity parts a string into all substrings after each example of the given separator and returns a cut which contains these substrings.

Syntax:

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

Here, str is the string, sep is the separator, and m is utilized to track down the quantity of substrings to return. Here, in the event that m>0, it returns all things considered m substrings and the last string substring won’t part. On the off chance that m == 0, it will bring nothing back. On the off chance that m<0, it will return all substrings.

Example:

// Go program to illustrate how to split a string
package main
 
import (
    "fmt"
    "strings"
)
 
// Main function
func main() {
 
    // Creating and initializing the strings
    str1 := "Welcome, to the, online portal, of GeeksforGeeks"
    str2 := "My dog name is Dollar"
    str3 := "I like to play Ludo"
 
    // Displaying strings
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    fmt.Println("String 3: ", str3)
 
    // Splitting the given strings
    // Using SplitAfterN() function
    res1 := strings.SplitAfterN(str1, ",", 2)
    res2 := strings.SplitAfterN(str2, "", 4)
    res3 := strings.SplitAfterN(str3, "!", 1)
    res4 := strings.SplitAfterN("", "GeeksforGeeks, geeks", 3)
 
    // 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:  Welcome, to the, online portal, of GeeksforGeeks
String 2:  My dog name is Dollar
String 3:  I like to play Ludo

Result 1:  [Welcome,  to the, online portal, of GeeksforGeeks]
Result 2:  [M y   dog name is Dollar]
Result 3:  [I like to play Ludo]
Result 4:  []

Also Read: How Raspberry Pi Is Better than Arduino For Learning Electronics

Leave a Reply

Your email address will not be published.