How to split string in GO by array of runes?

huangapple go评论116阅读模式
英文:

How to split string in GO by array of runes?

问题

如果你有一个由符文数组作为分隔符的字符串,有没有办法将它拆分成字符串数组?以下是我想要的一个示例:

separators = {' ',')','('}
SomeFunction("my string(qq bb)zz",separators) => {"my","string","qq","bb","zz"}

英文:

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:

  1. seperators = {' ',')','('}
  2. SomeFunction("my string(qq bb)zz",seperators) => {"my","string","qq","bb","zz"}

答案1

得分: 13

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func split(s string, separators []rune) []string {
  7. f := func(r rune) bool {
  8. for _, s := range separators {
  9. if r == s {
  10. return true
  11. }
  12. }
  13. return false
  14. }
  15. return strings.FieldsFunc(s, f)
  16. }
  17. func main() {
  18. separators := []rune{' ', ')', '('}
  19. s := "my string(qq bb)zz"
  20. ss := split(s, separators)
  21. fmt.Printf("%q\n", s)
  22. fmt.Printf("%q\n", ss)
  23. }

输出:

  1. "my string(qq bb)zz"
  2. ["my" "string" "qq" "bb" "zz"]
英文:

For example,

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func split(s string, separators []rune) []string {
  7. f := func(r rune) bool {
  8. for _, s := range separators {
  9. if r == s {
  10. return true
  11. }
  12. }
  13. return false
  14. }
  15. return strings.FieldsFunc(s, f)
  16. }
  17. func main() {
  18. separators := []rune{' ', ')', '('}
  19. s := "my string(qq bb)zz"
  20. ss := split(s, separators)
  21. fmt.Printf("%q\n", s)
  22. fmt.Printf("%q\n", ss)
  23. }

Output:

  1. "my string(qq bb)zz"
  2. ["my" "string" "qq" "bb" "zz"]

答案2

得分: 2

使用正则表达式:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var re = regexp.MustCompile(`[() ]`)
  7. func main() {
  8. text := "my string(qq bb)zz"
  9. splinedText := re.Split(text, -1)
  10. fmt.Printf("%q\n", text)
  11. fmt.Printf("%q\n", splinedText)
  12. }

输出:

  1. "my string(qq bb)zz"
  2. ["my" "string" "qq" "bb" "zz"]
英文:

with regexp:

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var re = regexp.MustCompile("[() ]")
  7. func main() {
  8. text := "my string(qq bb)zz"
  9. splinedText := re.Split(text, -1)
  10. fmt.Printf("%q\n", text)
  11. fmt.Printf("%q\n", splinedText)
  12. }

output:

  1. "my string(qq bb)zz"
  2. ["my" "string" "qq" "bb" "zz"]

答案3

得分: 0

我相信一个更简单的方法是使用函数FieldsFunc。以下是它的实现示例:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode"
  6. )
  7. func main() {
  8. f := func(c rune) bool {
  9. return !unicode.IsLetter(c) && !unicode.IsNumber(c)
  10. }
  11. fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
  12. }

输出结果:

Fields are: ["foo1" "bar2" "baz3"]

英文:

I believe that a simpler approach would be to use the function FieldsFunc. Here is an example of it's implementation:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode"
  6. )
  7. func main() {
  8. f := func(c rune) bool {
  9. return !unicode.IsLetter(c) && !unicode.IsNumber(c)
  10. }
  11. fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f))
  12. }

Output :
> Fields are: ["foo1" "bar2" "baz3"]

huangapple
  • 本文由 发表于 2014年3月4日 03:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/22155313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定