1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 14:42:27 +08:00

feat: add Ellipsis for string

This commit is contained in:
dudaodong
2024-09-03 14:36:12 +08:00
parent 9824db0056
commit 71e914019b
5 changed files with 140 additions and 12 deletions

View File

@@ -621,6 +621,8 @@ func HammingDistance(a, b string) (int, error) {
// Concat uses the strings.Builder to concatenate the input strings.
// - `length` is the expected length of the concatenated string.
// - if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number.
//
// Play: todo
func Concat(length int, str ...string) string {
if len(str) == 0 {
return ""
@@ -638,3 +640,21 @@ func Concat(length int, str ...string) string {
}
return sb.String()
}
// Ellipsis truncates a string to a specified length and appends an ellipsis.
// Play: todo
func Ellipsis(str string, length int) string {
str = strings.TrimSpace(str)
if length <= 0 {
return ""
}
runes := []rune(str)
if len(runes) <= length {
return str
}
return string(runes[:length]) + "..."
}