GoLang String Split Function

Learn via video courses
Topics Covered

Overview

In the Go language, strings are a sequence where each character represents one or more bytes encoded using UTF-8. In the Golang Split String, the strings can be split into slices with the help of in-built functions. The functions are defined in the string package and hence need to be imported before using the function.

Introduction

The method split() in the Golang Split String is defined as a string library that breaks the string into a list of substrings by using a specified separator. The split() method returns the output of the substring in the form of the slice.

Syntax of GoLang String Split Function

The syntax of how the split function is defined is as follows:

Here, str is the string that is to be split sep is a separator that splits the string at the specified sep. if as a separator an empty string is provided then the string splits at every character of the string.

Go Program to Illustrate Golang Split String

The code given below shows how the split method is used for Golang Split String.

Output:

Explanation: In this example, the string is split according to the commas and hence in the str mentioned, hi, this is and the scaler is separated based on commas and hence the length of the slice comes out to be 3.

SplitAfter

This function is used for golang string split into different substrings after every instance of the separator and then returns a slice as an output that contains all these different substrings.

Syntax

The syntax for this is as shown below:

Here, The str is a string and the sep is a separator If the string str does not contain the sep that is given and sep is not empty then at the output, this function will return the slice of length 1 which contains string str only. If the sep is empty, then, in that case, it will split after every UTF-8 sequence while if both str and sep are empty then in that case it will return an empty slice.

Example

Write a Go program to illustrate golang string split

Output

SplitAfterN

This function is used for golang string split into different substrings after every instance of the separator that is given and returns a slice as an output that contains all these substrings.

Syntax

The syntax for this function is as shown below:

Here, the str is the string while sep is the separator m is an integer that is used to find several substrings that are to be returned. If m>0 then this function returns at the most m substrings where the last substring will not be split. In the case of m==0, this function would return nil. If m<0 then, in this case, it would return all the substrings present.

Example

Write a Go program to illustrate how to split a string

Output

Split by a Comma or other Delimiter

In this, we will use strings.SplitN() function that takes following three arguments:

  • The string that is to be split
  • A separator
  • The number of resultant string in the slice

Output:

Explanation: This function is mostly used when we are working on a large document and are only interested in starting part of the text.

Split by Whitespace and Newline

The string package does more than just the split string function. The function strings.Fields() is used for separating a string on every whitespace. It also excludes those whitespaces from the final output. This function is only useful when whitespaces are not a priority. For example, tabs, spaces, and newlines are all counted as a space in this function.

Output:

Split on Regular Expression

We can also split strings by using regular expressions. These are the most popular way of manipulating a string. The built-in engine of Go’s regex helps to split the string by using regex or regular expression. The regexp package is used instead of the strings package.

Output:

String Split without Removing Delimiters

The function SplitAfter can be used for retaining the delimiter instead of just removing it. The code given below shows how to split the string using this function.

Output:

This function can however be used for generating a slice with all the individual characters of strings in it. This can better be understood from the example given below:

Output:

Conclusion

  • In the Go language, strings are a sequence where each character represents one or more bytes encoded using UTF-8. In the Go language, the strings can be split into slices with the help of in-built functions. The functions are defined in the string package and hence need to be imported before using the function.
  • The method split() in the Go language is defined as a string library that breaks the string into a list of substrings by using a specified separator. The split() method returns the output of the substring in the form of the slice.
  • The syntax of how the split function is defined is as follows: func Split(str, sep string) []string