Golang : Word limiter example
Problem:
You want to limit a string to X number of words. How to do that?
Solution:
Break the string into a slice of words, count the total number of words and join the words into a new string again but limited to the specified X number.
For example:
package main
import (
"fmt"
"strings"
)
// Limits a string to X number of words.
func word_limiter(s string, limit int) string {
if strings.TrimSpace(s) == "" {
return s
}
// convert string to slice
strSlice := strings.Fields(s)
// count the number of words
numWords := len(strSlice)
var result string
if numWords > limit {
// convert slice/array back to string
result = strings.Join(strSlice[0:limit], " ")
// the three dots for end characters are optional
// you can change it to something else or remove this line
result = result + "..."
} else {
// the number of limit is higher than the number of words
// return default or else will cause
// panic: runtime error: slice bounds out of range
result = s
}
return string(result)
}
func main() {
str := "Here is a nice text string consisting of eleven words."
// limit to 3 words
fmt.Println(word_limiter(str, 3))
// limit to 8 words
fmt.Println(word_limiter(str, 8))
// limit to 18 words
// will return all words because limit is higher than the number of
// words
fmt.Println(word_limiter(str, 18))
unicodeStr := "世界你好"
// limit to 3 words -- will not work!
// characters need to be separated by spaces
fmt.Println(word_limiter(unicodeStr, 3))
unicodeStr2 := "世 界 你 好"
// limit to 3 words
// -- will work because characters are separated by spaces
fmt.Println(word_limiter(unicodeStr2, 3))
}
Output:
Here is a...
Here is a nice text string consisting of...
Here is a nice text string consisting of eleven words.
世界你好
世 界 你...
References:
https://www.socketloop.com/tutorials/golang-append-and-add-item-in-slice
https://github.com/bcit-ci/CodeIgniter/blob/develop/system/helpers/text_helper.php
See also : Golang : Count number of runes in string
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+19.9k Golang : How to get struct tag and use field name to retrieve data?
+11.4k CodeIgniter : Import Linkedin data
+7.4k Gogland : Where to put source code files in package directory for rookie
+13.8k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+13k Golang : Date and Time formatting
+11.8k Golang : Save webcamera frames to video file
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+8.3k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+31k Golang : bufio.NewReader.ReadLine to read file line by line
+7.1k Golang : Individual and total number of words counter example
+3.8k Detect if Google Analytics and Developer Media are loaded properly or not
+19.3k Golang : How to Set or Add Header http.ResponseWriter?