Skip to content

Instantly share code, notes, and snippets.

@xlqstar
Created September 1, 2013 12:26
Show Gist options
  • Save xlqstar/6404158 to your computer and use it in GitHub Desktop.
Save xlqstar/6404158 to your computer and use it in GitHub Desktop.
//**使用(多个)空格分割字符串 Fields(s string) ,返回分割后的数组 **
将字符串分割成数组,其分割符为空格。
查看示例代码
fmt.Println(strings.Fields("lao Yu Study Go ")) //OutPut: [lao Yu Study Go]
fmt.Println(strings.Fields(" Go ")) //[Go]
fmt.Println(strings.Fields("")) //[]
fmt.Println(strings.Fields(" \n go")) //[go]
//**其实其内部实现调用的是FieldsFunc(s,unicode.IsSpace),我们也可以自定义分割方式 **
canSplit := func (c rune) bool { return c=='#'}
fmt.Println(strings.FieldsFunc("lao###Yu#Study####Go#G ",canSplit)) //[lao Yu Study Go G<space>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment