If there is any way to split string into array of strings, when you have as a separator an array of runes? There is an example what I want:
seperators = {' ',')','('}
SomeFunction("my string(qq bb)zz",seperators) => {"my","string","qq","bb","zz"}
The Split() method in Golang (defined in the strings library) breaks a string down into a list of substrings using a specified separator. The method returns the substrings in the form of a slice.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
To convert a string into array of characters (which is slice of runes) in Go language, pass the string as argument to []rune(). This will create a slice of runes where each character is stored as an element in the resulting slice.
strings.Split() function. The Split function takes a string and a delimiter as parameters and returns a slice of strings where each substring was formally separated by the given delimiter.
For example,
package main
import (
"fmt"
"strings"
)
func split(s string, separators []rune) []string {
f := func(r rune) bool {
for _, s := range separators {
if r == s {
return true
}
}
return false
}
return strings.FieldsFunc(s, f)
}
func main() {
separators := []rune{' ', ')', '('}
s := "my string(qq bb)zz"
ss := split(s, separators)
fmt.Printf("%q\n", s)
fmt.Printf("%q\n", ss)
}
Output:
"my string(qq bb)zz"
["my" "string" "qq" "bb" "zz"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With