ADVERTISEMENT
ADVERTISEMENT

Golang strings.Fields() Function with Examples

Golang | strings.Fields() Function: Here, we are going to learn about the Fields() function of strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 15, 2021

strings.Fields()

The Fields() function is an inbuilt function of strings package which is used to split the given string into a slice of substrings separated by white spaces. It accepts a string parameter and returns a slice of substrings if the string is there, otherwise, it returns an empty slice if the string contains only white spaces.

Syntax:

func Fields(str string) []string

Parameter(s):

  • str: String from which we have to find the slice of substrings.

Return Value:

The return type of Fields() function is a []string, it returns a slice of substrings if the string is there, otherwise, it returns an empty slice if the string contains only white spaces.

Example 1:

// Golang program to demonstrate the
// example of strings.Fields() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	// printing return value
	fmt.Println(strings.Fields("Hello world"))
	fmt.Println(strings.Fields("Is are am"))
	fmt.Println(strings.Fields("Hello, world! How are you?"))
	fmt.Println(strings.Fields("   abc   pqr    xyz"))
	fmt.Println(strings.Fields("    "))

	// printing slice of substring with double-quotes
	fmt.Printf("Fields are: %q\n", strings.Fields("Hello world"))
	fmt.Printf("Fields are: %q\n", strings.Fields("Is are am"))
	fmt.Printf("Fields are: %q\n", strings.Fields("Hello, world! How are you?"))
	fmt.Printf("Fields are: %q\n", strings.Fields("   abc   pqr    xyz"))
	fmt.Printf("Fields are: %q\n", strings.Fields("    "))
}

Output:

[Hello world]
[Is are am]
[Hello, world! How are you?]
[abc pqr xyz]
[]
Fields are: ["Hello" "world"]
Fields are: ["Is" "are" "am"]
Fields are: ["Hello," "world!" "How" "are" "you?"]
Fields are: ["abc" "pqr" "xyz"]
Fields are: []

Example 2:

// Golang program to demonstrate the
// example of strings.Fields() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var slices []string

	str = "Hello world"
	slices = strings.Fields(str)
	fmt.Printf("%q contains these fields %q\n", str, slices)

	str = "Is are am"
	slices = strings.Fields(str)
	fmt.Printf("%q contains these fields %q\n", str, slices)

	str = "Hello, world! How are you?"
	slices = strings.Fields(str)
	fmt.Printf("%q contains these fields %q\n", str, slices)

	str = "   abc   pqr    xyz"
	slices = strings.Fields(str)
	fmt.Printf("%q contains these fields %q\n", str, slices)

	str = "    "
	slices = strings.Fields(str)
	fmt.Printf("%q contains these fields %q\n", str, slices)
}

Output:

"Hello world" contains these fields ["Hello" "world"]
"Is are am" contains these fields ["Is" "are" "am"]
"Hello, world! How are you?" contains these fields ["Hello," "world!" "How" "are" "you?"]
"   abc   pqr    xyz" contains these fields ["abc" "pqr" "xyz"]
"    " contains these fields []

Golang strings Package Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.