如何使用分隔符切割字符串

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

How to slice a string using a delimiter

问题

var s string = "a,b,c,d,e"

arr := strings.Split(s, ",")

英文:

In Go, if I have a string variable s:

var s string = "a,b,c,d,e"

How can I convert or split or explode it into a slice or an array of strings so that it will become:

arr[0] = "a"
...
arr[4] = "e"

答案1

得分: 83

你应该使用strings包来实现。

stringSlice := strings.Split(s, ",")

http://play.golang.org/p/UKZbcuJUPP

英文:

You should use the strings package for that.

stringSlice := strings.Split(s, ",")

http://play.golang.org/p/UKZbcuJUPP

答案2

得分: 2

如果你想忽略空元素,你可以使用strings#FieldsFunc

package main

import (
   "fmt"
   "strings"
)

func main() {
   s := ",a,,b,c,d,e,"
   arr := strings.FieldsFunc(s, func(r rune) bool {
      return r == ','
   })
   fmt.Printf("%q\n", arr) // ["a" "b" "c" "d" "e"]
}

或者使用bufio#Scanner.Split

package main

import (
   "bufio"
   "strings"
)

func comma(data []byte, eof bool) (int, []byte, error) {
   if eof { return 0, nil, nil }
   a := -1
   for b, c := range data {
      if c == ',' {
         if a >= 0 {
            return b+1, data[a:b], nil
         }
      } else if a < 0 {
         a = b
      }
   }
   return len(data), data, nil
}

func main() {
   s := ",a,,b,c,d,e"
   arr := bufio.NewScanner(strings.NewReader(s))
   arr.Split(comma)
   for arr.Scan() {
      println(arr.Text())
   }
}
英文:

If you want to ignore empty elements, you can use
strings#FieldsFunc:

package main

import (
   &quot;fmt&quot;
   &quot;strings&quot;
)

func main() {
   s := &quot;,a,,b,c,d,e,&quot;
   arr := strings.FieldsFunc(s, func(r rune) bool {
      return r == &#39;,&#39;
   })
   fmt.Printf(&quot;%q\n&quot;, arr) // [&quot;a&quot; &quot;b&quot; &quot;c&quot; &quot;d&quot; &quot;e&quot;]
}

or bufio#Scanner.Split:

package main

import (
   &quot;bufio&quot;
   &quot;strings&quot;
)

func comma(data []byte, eof bool) (int, []byte, error) {
   if eof { return 0, nil, nil }
   a := -1
   for b, c := range data {
      if c == &#39;,&#39; {
         if a &gt;= 0 {
            return b+1, data[a:b], nil
         }
      } else if a &lt; 0 {
         a = b
      }
   }
   return len(data), data, nil
}

func main() {
   s := &quot;,a,,b,c,d,e&quot;
   arr := bufio.NewScanner(strings.NewReader(s))
   arr.Split(comma)
   for arr.Scan() {
      println(arr.Text())
   }
}

答案3

得分: 0

尝试这个:

package main

import "fmt"

func main() {
var s string = "a,b,c,d,e"
var arr []string

start := 0
for i := 0; i < len(s); i++ {
	if s[i] == ',' {
		arr = append(arr, s[start:i])
		start = i + 1
	}
}

if start < len(s) {
	arr = append(arr, s[start:])
}
fmt.Print(arr)
}
英文:

Try this one:

package main

import &quot;fmt&quot;

func main() {
var s string = &quot;a,b,c,d,e&quot;
var arr []string

start := 0
for i := 0; i &lt; len(s); i++ {
	if s[i] == &#39;,&#39; {
		arr = append(arr, s[start:i])
		start = i + 1
	}
}

if start &lt; len(s) {
	arr = append(arr, s[start:])
}
fmt.Print(arr)
}

huangapple
  • 本文由 发表于 2013年1月11日 01:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/14263733.html
匿名

发表评论

匿名网友

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

确定