aqi/utils/string_limit.go
2024-06-18 18:09:39 +08:00

15 lines
326 B
Go

package utils
func LimitString(str string, limit int) string {
// Convert the string to a rune slice to properly handle Unicode characters
runes := []rune(str)
if len(runes) > limit {
// If the length exceeds the limit
// truncate the slice to the specified length
runes = runes[:limit]
}
return string(runes)
}