How to Split A String By A Delimiter In Golang?

10 minutes read

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:
1
import "strings"


  1. Use the Split function from the strings package to split the string:
1
2
3
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:
1
2
3
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.

Best Golang Books to Learn of 2024

1
Learning Go: An Idiomatic Approach to Real-World Go Programming

Rating is 5 out of 5

Learning Go: An Idiomatic Approach to Real-World Go Programming

2
Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

Rating is 4.9 out of 5

Mastering Go: Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures, 2nd Edition

3
Learn Data Structures and Algorithms with Golang: Level up your Go programming skills to develop faster and more efficient code

Rating is 4.8 out of 5

Learn Data Structures and Algorithms with Golang: Level up your Go programming skills to develop faster and more efficient code

4
Go Programming Language, The (Addison-Wesley Professional Computing Series)

Rating is 4.7 out of 5

Go Programming Language, The (Addison-Wesley Professional Computing Series)

5
Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

Rating is 4.6 out of 5

Event-Driven Architecture in Golang: Building complex systems with asynchronicity and eventual consistency

6
Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

Rating is 4.5 out of 5

Distributed Services with Go: Your Guide to Reliable, Scalable, and Maintainable Systems

7
Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code

Rating is 4.4 out of 5

Functional Programming in Go: Apply functional techniques in Golang to improve the testability, readability, and security of your code

8
100 Go Mistakes and How to Avoid Them

Rating is 4.3 out of 5

100 Go Mistakes and How to Avoid Them

9
Head First Go

Rating is 4.2 out of 5

Head First Go

10
Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang

Rating is 4.1 out of 5

Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang


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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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:

1
2
3
4
5
6
7
hello
world!
this
is
a
test
string.


Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To split a string with a space in Java, you can use the built-in split() method of the String class. The split() method allows you to divide a string into an array of substrings based on a given delimiter or regular expression.To split a string with a space sp...
To add quotes to a Java string, you can use the escape character "" to indicate that the quote should be included as part of the string itself. Here are a few examples:Adding double quotes to a string: String str1 = "This is a "quoted" stri...
To add a number as a string to a string in Haskell, you can use the show function to convert the number to a string and then concatenate it with the existing string using the ++ operator. Here's an example: addNumberToString :: String -> Int -> Strin...