1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

add Concat method (#204)

* feat(strutil): add Concat method

* feat(strutil): add Concat method
This commit is contained in:
Cannian
2024-03-25 10:26:37 +08:00
committed by GitHub
parent bb6f10a1fb
commit e9280b8c25
5 changed files with 124 additions and 2 deletions

View File

@@ -617,3 +617,24 @@ func HammingDistance(a, b string) (int, error) {
return distance, nil
}
// 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.
func Concat(length int, str ...string) string {
if len(str) == 0 {
return ""
}
sb := strings.Builder{}
if length <= 0 {
sb.Grow(len(str[0]) * len(str))
} else {
sb.Grow(length)
}
for _, s := range str {
sb.WriteString(s)
}
return sb.String()
}