Home
Go
Word Count
Updated May 30, 2023
Dot Net Perls
Word count. This sentence has a certain number of words. But how many? With a Go method, we can count sequences of non-whitespace characters—these are words.
With a word count method, we can generate statistics about text. We can verify that it is correct. A regexp can be used for the most accurate word counting.
Example func. To write a simple word count func in Go, we can use regexp. Think of a series of words—the one thing we can do to detect a word is find a sequence of letters or digits.
And Letters or digits are non-whitespace characters. We can use a special regexp metacharacter to detect these.
Detail In WordCount we invoke FindAllString with a -1 argument—this finds all matches.
Finally We return the length of the results found by FindAllString. This is our entire word counting method.
package main import ( "fmt" "regexp" ) func WordCount(value string) int { // Match non-space character sequences. re := regexp.MustCompile(`[\S]+`) // Find all matches and return count. results := re.FindAllString(value, -1) return len(results) } func main() { // This has 10 words. fmt.Println(WordCount("To be or not to be, that is the question.")) // This has 1 word. fmt.Println(WordCount("Hello")) // This has 2 words. fmt.Println(WordCount("Hello friend")) }
10 1 2
Notes, results. With some simple string tests, we can see that the results appear correct. The string "Hello" has just 1 word. The string "Hello friend" has 2.
Handling punctuation. An important thing to get right in word counting I show punctuation separates words. A double-hyphen can separate 2 words—the regexp must handle this case.
A review. For an easy-to-write and fairly accurate word counting method, counting sequences of 1 or more non-whitespace chars is a good option. More advanced approaches are possible.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen