Truncates a string up to a specified length.

Use len() to determine if the length is greater than l.
Return the string truncated to the desired length, with "..." appended to the end or the original string.

代码实现

func TruncateString(s string, l int) string {
    r := s
    if len(s) > l {
        if l > 3 {
            l -= 3
        }
        r = s[0:l] + "..."
    }
    return r
}

使用样例

TruncateString("boomerang", 7); // "boom..."