GopherSnippetsStar

Code snippets with tests and testable examples for the Go programming language

How to count the number of words in a string

Snippets Index - Run code on Go playground - Edit

package main

import (
	"fmt"
	"strings"
)

// strings.Fields() splits a string around one or more
// consecutive whitespaces (https://golang.org/pkg/unicode/#IsSpace)
// The number of fields is the actual number of words
func CountWords(s string) int {
	return len(strings.Fields(s))
}

// If the words are not separated by whitespaces
// but with comma (a csv row for example) or whatever else,
// you can use the strings.FieldsFunc() and explicitly define
// the points of split
func CountWordsFunc(s string, f func(rune) bool) int {
	return len(strings.FieldsFunc(s, f))
}

func ExampleCountWords() {
	wc := CountWords("  How  to count words? \n")
	fmt.Println(wc)
	// Output:
	// 4
}

func ExampleCountWordsFunc() {
	f := func(c rune) bool {
		return c == ','
	}
	wc := CountWordsFunc("word1, word2, word3", f)
	fmt.Println(wc)
	// Output:
	// 3
}

by psampaz - source code - comment below or here