TruncateStr truncates s to at most n runes, safe for multi-byte (e.g. CJK) characters.
(s string, n int)
| 5 | |
| 6 | // TruncateStr truncates s to at most n runes, safe for multi-byte (e.g. CJK) characters. |
| 7 | func TruncateStr(s string, n int) string { |
| 8 | if n <= 0 { |
| 9 | return "" |
| 10 | } |
| 11 | r := []rune(s) |
| 12 | if len(r) <= n { |
| 13 | return s |
| 14 | } |
| 15 | return string(r[:n]) |
| 16 | } |
| 17 | |
| 18 | // TruncateStrWithEllipsis truncates s to at most n runes (including "..." suffix). |
| 19 | func TruncateStrWithEllipsis(s string, n int) string { |
no outgoing calls