strings.Fields() Golang中的函数用于在unicode.IsSpace定义的一个或多个连续空白字符的每个实例周围拆分给定的字符串,返回str的子字符串切片或如果str仅包含空白的空白切片。
用法:
func Fields(str string) []string
返回值:str的子字符串切片或如果str仅包含空格的空白切片。
范例1:
// Golang program to illustrate the
// strings.Fields() Function
package main
import (
"fmt"
"strings"
)
func main() {
// String s is split on the basis of white spaces
// and store in a string array
s:= "GeeksforGeeks is a computer science portal !"
v:= strings.Fields(s)
fmt.Println(v)
// Another example by passing the string as argument
// directly to the Fields() function
v = strings.Fields("I am a software developer, I love coding")
fmt.Println(v)
}
输出:
[GeeksforGeeks is a computer science portal !] [I am a software developer, I love coding]
范例2:
// Golang program to illustrate the
// strings.Fields() Function
package main
import (
"fmt"
"strings"
)
func main() {
// Fields function also splits the string
// on the basis of white spaces and tabs
s:= strings.Fields(" I \t love \n to \n code \n all \t day.")
fmt.Println(s)
// Splits into 5 words which have
// newline character in between
s = strings.Fields("I\nam\nlearning\nGo\nlanguage")
fmt.Println(s)
}
输出:
[I love to code all day.] [I am learning Go language]
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang time.Time.Year()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
注:本文由纯净天空筛选整理自vanigupta20024大神的英文原创作品 strings.Fields() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。