HUGO
Menu
GitHub 90272 stars Mastodon

strings.CountWords

Returns the number of words in the given string, excluding HTML tags.
Syntax
strings.CountWords STRING
Returns
int
Alias
countwords

Usage

The strings.CountWords function returns the number of words in the given string, excluding HTML tags. If the string contains CJK characters, the function counts each of those characters as a word.

To count the words in the content of a page, use the WordCount method.

The strings.CountWords function applies CJK word counting rules to any string containing CJK characters, regardless of the hasCJKLanguage setting in your project configuration or the isCJKLanguage field in front matter. As a result, the value returned by this function may differ from the value returned by the WordCount method.

Examples

{{ "Hugo is a static site generator." | strings.CountWords }} → 6

Counting words in a string that contains HTML tags:

{{ "<p>Hugo is a <em>static</em> site generator.</p>" | strings.CountWords }} → 6

Counting words in a string that contains Chinese characters:

{{ "Hugo 是一个静态网站生成器" | strings.CountWords }} → 11