Home » Golang Strings SplitN function with Examples

Golang Strings SplitN function with Examples

Golang strings.SplitN() function split a given string into substrings separated by a separator and it returns a slice of substrings between those separators.

Syntax

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

Where,

s = given string

sep = is the delimiter

n = count that defines the number of substrings to be returned.

Output: Returns string array.

If n = 0, SplitN will returns empty list. Zero substrings.

If n < 0, SplitN returns all substrings.

if n > 0, SplitN returns n substrings

In this article, we will discuss how to use strings.SplitN() function in golang to split a string into substrings by the specified separator.

Golang Split String by strings.SplitN()

Golang strings package has a built-in SplitN method that takes three arguments; a string, separator, and count which defines the number of substrings to be returned.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Use the SplitN to split the string by comma
        // string = "C#,Golang,Python,C"
        // Seperator= ","
        // n = 2
	s1 := strings.SplitN("C#,Golang,Python,C", ",",2)
	fmt.Println(s1)
	fmt.Println("The length of the slice is:", len(s1))

}

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

Golang strings SplitN function takes 3 arguments:

  1. string “C#,Golang,Python,C”
  2. the separator as comma,
  3. n = 2 to returns at most 2 substrings

In the first example, strings.SplitN() function split a string by comma into 2 parts C# and Golang,Python,C and returns a slice of substrings ( string array).

The output of the above golang program to split a string by comma using the SplitN function is:

[C# Golang,Python,C]
The length of the slice is: 2

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

Golang SplitN Examples

Let’s understand Golang SplitN function with more examples.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Example1 - Split a string by - using SplitN
	s1 := strings.SplitN("01-23-45-67-89-ab-34-ad-21", "-", 0)
	fmt.Println(s1)
	fmt.Println("The length of the slice is:", len(s1))

	// Exmaple2 - Split a string by : using SplitN
	s2 := strings.SplitN("01:23:45:67:89:ab:34:ad:21", ":", -1)
	fmt.Println(s2)
	fmt.Println("The length of the slice is:", len(s2))

	// Example3 - Split a string by : using SplitN
	s3 := strings.SplitN("01:23:45:67:89:ab:34:ad:21", ":", 3)
	fmt.Println(s3)
	fmt.Println("The length of the slice is:", len(s3))

}

In the above Golang program, we have demonstrated strings.SplitN function with 3 different examples.

In the first example, a string is split by – using the SplitN function and n=0 as the last parameter. It returns the empty string as []

In the second example, a string is split by : using the SplitN function and n= -1 as the last parameter. It returns all substrings as [01 23 45 67 89 ab 34 ad 21]

In the third example, a string is split by : using the SplitN function, and n= 3 as the last parameter. It returns 3 substrings as 01, 23 and 45:67:89:ab:34:ad:21

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

[]
The length of the slice is: 0
[01 23 45 67 89 ab 34 ad 21]
The length of the slice is: 9
[01 23 45:67:89:ab:34:ad:21]
The length of the slice is: 3

Conclusion

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

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

Leave a Comment