Home ยป How to Split String into Array in Golang

How to Split String into Array in Golang

Use the Golang strings.Split() function to split a string into an array by separator and returns a slice of the substrings.

The Split() function takes two arguments; a string and a separator to split a string into an array.

In this article, we will discuss how to split a string into an array in golang and split a string into characters.

How to Split String to Array Using Split() in Golang

To split a string into an array, use the Split() function in Golang.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Declare a string
	s1 := "C#,Java,Pascal,Go,Jquery,R,VB,Python"

	// Use Split() function to split the string by colon (,)

	strArr := strings.Split(s1, ",")

	// use for loop to iterate the string array

	for i := 0; i < len(strArr); i++ {

		fmt.Println(strArr[i])
	}

	// Print the length of array
	fmt.Println("Length of the array:", len(strArr))

	// Get the first element of the array

	fmt.Println("First Element of the Array:", strArr[0])
}

In the above Golang program, we have used strings.Split() function to split a string into an array.

To Split a string into an array using the Split() function, follow the below steps:

  1. Declare a variable s1 and store the comma-separated values in it.
  2. Use the Split() function to split the string s1 with separator comma (,)
  3. Use the for loop to iterate over the split string array.
  4. Print each of the elements from the array using fmt.Println()
  5. Use the len() function to get the length of the string split by the Split() function.
  6. To get the first element of the string split, use the index e.g. strArr[0]

The output of the above golang program to split a string into an array is:

C#
Java
Pascal
Go
Jquery
R
VB
Python
Length of the array: 8
First Element of the Array: C#

Cool Tip: How to use the golang next function of the list package!

Split String into Array of Characters in Golang

You can split a string into characters using the strings.Split() function in golang.

The Split() function takes a string as input and an empty separator to split a string into a slice of characters.

package main

import (
	"fmt"
	"strings"
)

func main() {

	// Declare a string
	s1 := "GolangSpot"

	// Use Split() function to split the string by colon (,)

	strArr := strings.Split(s1, "")

	// Print the split string array
	fmt.Println(strArr)

	// use for loop to iterate the characters array

	for i := 0; i < len(strArr); i++ {

		fmt.Println(strArr[i])
	}

	// Print the length of array
	fmt.Println("Length of the array:", len(strArr))

	// Get the first element of the array

	fmt.Println("First Element of the Array:", strArr[0])
}

In the above Golang program, we have used strings.Split() function to split a string into characters.

In the Split function, it takes the string s1 and the empty separator. If the separator is empty, split a string after each character or UTF-8 sequence.

We used fmt.Println to print the characters array.

The output of the above program to split a string into characters in golang is:

[G o l a n g S p o t]
G
o
l
a
n
g
S
p
o
t
Length of the array: 10
First Element of the Array: G

Cool Tip: How to split a string into two variables in Golang!

Conclusion

I hope the above article on how to use strings.Split() function to split a string into an array and split a string into a characters array is helpful to you.

Cool Tip: How to use the string Index function in Golang!

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

Leave a Comment