Golang Split String () - Examples

[Pages:3]Golang Split String ? strings.Split() ? Examples

Golang Split String

To split a string in Golang, you can use strings.Split() function. The syntax of strings.Split() function is:

strings.Split(str, sep_string)

where

1. strings is the package 2. Split is the keyword 3. str is the input string 4. sep_string is the delimiter or separator

strings.Split() function returns string array []string .

In this tutorial, we will learn how to split a string in Golang using a delimiter,

Example 1 ? Split String in Golang

In this example, we will take a string and split it with hyphen - as delimiter.

example.go

package main import (

"fmt" "strings" ) func main() { var splits = strings.Split("annbnnc", "nn") fmt.Printf("%q\n", splits) }

Output

C:\workspace\go>go run example.go ["a" "b" "c"]

Example 2 ? Split String with Comma Separated Values

In this example, we will take a string containing values separated by comma. And we will split it with hyphen , as delimiter.

example.go

package main import (

"fmt" "strings" ) func main() { var splits = strings.Split("a,b,c", ",") fmt.Printf("%q\n", splits) }

Output

C:\workspace\go>go run example.go ["a" "b" "c"]

Conclusion

In this Golang Tutorial, we learned how to split a string using a delimiter.

Golang Golang Tutorial Run Golang Program Golang If Else Golang Switch Golang For Loop Golang Comments Golang Functions

Golang Array Golang Slice Golang Struct Golang Class Golang Range Golang Map Golang Goroutine Golang Channel

Golang String Operations Golang String Length Golang String Concatenation Golang Split String Golang String - Get Index of Substr Golang String to Uppercase Golang String to Lowercase Golang Convert String to Integer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download