I'd like to make teaser by cutting first few words (divided by spaces) from arbitrary utf8 input string content
. What I came up with is this:
runes := []rune(content)
teaser := string(runes[0:75])
The problem is that the above code cuts words in the middle. What I want is to cut at the end of (say tenth) word, in order to make pretty teasers.
How can I achieve that?
func teaser(s string, wordCount int) string {
words := strings.Fields(s)
if len(words) < wordCount {
wordCount = len(words)
}
return strings.Join(words[:wordCount], " ")
}
... where s
is your full string, and wordCount
the number of words to include.