strings.Fields() Function in Golang is used to splits the given string around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of str or an empty slice if str contains only white space.
Syntax:
func Fields(str string) []string
Returns: A slice of substrings of str or an empty slice if str contains only white space.
Example 1:
package main
import (
"fmt"
"strings"
)
func main() {
s := "GeeksforGeeks is a computer science portal !"
v := strings.Fields(s)
fmt.Println(v)
v = strings.Fields( "I am a software developer, I love coding" )
fmt.Println(v)
}
|
Output:
[GeeksforGeeks is a computer science portal !]
[I am a software developer, I love coding]
Example 2:
package main
import (
"fmt"
"strings"
)
func main() {
s := strings.Fields( " I \t love \n to \n code \n all \t day." )
fmt.Println(s)
s = strings.Fields( "I\nam\nlearning\nGo\nlanguage" )
fmt.Println(s)
}
|
Output:
[I love to code all day.]
[I am learning Go language]