Skip to main content
TopMiniSite

Back to all posts

How to Split A String By A Delimiter In Golang?

Published on
4 min read

Table of Contents

Show more
How to Split A String By A Delimiter In Golang? image

In Golang, you can split a string by a delimiter using the strings package. Here is a general approach to split a string:

  1. Import the strings package:

import "strings"

  1. Use the Split function from the strings package to split the string:

str := "Hello,World,How,Are,You" delimiter := "," result := strings.Split(str, delimiter)

In this example, the Split function takes two arguments: the string you want to split (str) and the delimiter you want to use for splitting (delimiter). In this case, the delimiter is a comma (,).

  1. The Split function will return a slice of substrings. You can then use this slice to access the individual elements that were separated by the delimiter:

for _, element := range result { // Do something with each element }

In this loop, each element of the result slice will be printed or processed as needed. The underscore (_) is used to ignore the index of the element, as we are only interested in the values.

That's how you can split a string by a delimiter in Golang using the strings package.

What is the function to split a string by a custom delimiter in Golang?

In Golang, the function Split() from the strings package can be used to split a string by a custom delimiter.

The syntax to split a string using a custom delimiter is: strings.Split(str, delimiter)

Here is an example of splitting a string by a custom delimiter:

package main

import ( "fmt" "strings" )

func main() { str := "Hello,World!This,is,a,string" delimiter := ","

parts := strings.Split(str, delimiter)

// Displaying the splitted parts
for \_, part := range parts {
	fmt.Println(part)
}

}

Output:

Hello World!This is a string

In the above example, the strings.Split() function is used to split the string str by the delimiter ,. The parts of the split string are then displayed using a for loop.

What is the function to split a string and convert each substring to an integer in Golang?

In Golang, you can use the strings.Split() function to split a string into substrings and then use the strconv.Atoi() function to convert each substring to an integer.

Here's an example of how you can do this:

package main

import ( "fmt" "strconv" "strings" )

func main() { str := "10 20 30 40 50" substrings := strings.Split(str, " ")

var numbers \[\]int
for \_, sub := range substrings {
	num, err := strconv.Atoi(sub)
	if err != nil {
		fmt.Printf("Error converting substring '%s' to int: %v\\n", sub, err)
		continue
	}
	numbers = append(numbers, num)
}

fmt.Println(numbers)

}

This program splits the input string "10 20 30 40 50" into substrings using the space as the delimiter, and then converts each substring to an integer using strconv.Atoi(). The resulting integers are stored in the numbers slice.

What is the purpose of the TrimSpace function in Golang?

The TrimSpace function in Golang is used to remove leading and trailing white spaces from a string. Its purpose is to clean up and normalize strings by removing any excess spaces that may cause inconsistencies or issues in further processing or comparisons. It is commonly used in data validation or when processing user input.

How to split a string and convert each substring to lowercase in Golang?

To split a string and convert each substring to lowercase in Go, you can use the strings.Split() function to split the string into an array of substrings, and then loop through the array and use the strings.ToLower() function to convert each substring to lowercase.

Here's an example:

package main

import ( "fmt" "strings" )

func main() { str := "Hello World! This is a TEST String."

// Splitting the string based on whitespace
substrings := strings.Split(str, " ")

// Converting each substring to lowercase
for i, sub := range substrings {
	substrings\[i\] = strings.ToLower(sub)
}

// Printing the modified substrings
for \_, sub := range substrings {
	fmt.Println(sub)
}

}

In this example, we start by defining the original string as str. Then we use strings.Split() to split the string into an array of substrings based on whitespace. Next, we loop through the array of substrings and use strings.ToLower() to convert each substring to lowercase.

Finally, we print each modified substring using a separate loop. The output will be:

hello world! this is a test string.