add Concat method (#204)

* feat(strutil): add Concat method

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

No files matched your search

+21
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()
}