How to Split a String in Golang? (original) (raw)

Last Updated : 12 Jul, 2025

In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the strings package. In this article,we will learn How to Split a String in Golang?

Example

package main
import (
"fmt"
"strings"
)

func main() {
str := "Welcome,to,GeeksforGeeks"
fmt.Println("Original String:", str)
}

Here, the string "Welcome,to,GeeksforGeeks" will be split using different methods. First, ensure you have imported the strings package to access the split functions.

Syntax

**func Split(s, sep string) []string #Using Split Function
**func SplitAfter(s, sep string) []string #Using SplitAfter Function
**func SplitN(s, sep string, n int) []string # Using SplitN Function
**func SplitAfterN(s, sep string, n int) []string # Using SplitAfterN Function

Table of Content

Using Split Function

The Split function divides a string into all substrings separated by the specified separator and returns a slice with these substrings.

**Syntax

**func Split(s, sep string) []string

**Example

Go `

package main import ( "fmt" "strings" )

func main() { s := "Welcome,to,GeeksforGeeks" fmt.Println("", s)

result := strings.Split(s, ",")
fmt.Println("Result:", result)

}

`

Output

Welcome,to,GeeksforGeeks Result: [Welcome to GeeksforGeeks]

SplitAfter Function

The SplitAfter function splits a string after each instance of the specified separator and returns a slice.

**Syntax:

func SplitAfter(s, sep string) []string

**Note:

Example

Go `

package main import ( "fmt" "strings" )

func main() { s := "Welcome,to,GeeksforGeeks" fmt.Println("", s)

result := strings.SplitAfter(s, ",")
fmt.Println("Result:", result)

}

`

Output

Welcome,to,GeeksforGeeks Result: [Welcome, to, GeeksforGeeks]

SplitN Function

The SplitN function splits a string into a maximum number of substrings.

**Syntax:

func SplitN(s, sep string, n int) []string

**Example:

Go `

package main import ( "fmt" "strings" )

func main() { s := "Welcome,to,GeeksforGeeks" fmt.Println("", s)

result := strings.SplitN(s, ",", 2)
fmt.Println("Result:", result)

}

`

Output

Welcome,to,GeeksforGeeks Result: [Welcome to,GeeksforGeeks]

SplitAfterN Function

The SplitAfterN function splits a string after each instance of the separator, but only up to n substrings.

**Syntax

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

**Example:

Go `

package main import ( "fmt" "strings" )

func main() { s := "Welcome,to,GeeksforGeeks" fmt.Println("", s)

result := strings.SplitAfterN(s, ",", 2)
fmt.Println("Result using SplitAfterN:", result)

}

`

Output

Welcome,to,GeeksforGeeks Result using SplitAfterN: [Welcome, to,GeeksforGeeks]

Each of these functions provides a flexible way to split strings in Go, depending on the use case and the desired output structure. By understanding these functions, you can more effectively handle string manipulation in your Go programs.