字符串拼接

This commit is contained in:
xing 2022-09-16 18:21:54 +08:00
parent 865e2e5725
commit 0c41cd5bcf
2 changed files with 34 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package helper
import (
"reflect"
"strings"
)
func IsContainInArr[T comparable](a T, arr []T) bool {
@ -31,3 +32,16 @@ func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](sta
}
return r
}
func StrJoin(s ...string) (str string) {
if len(s) == 1 {
return s[0]
} else if len(s) > 1 {
b := strings.Builder{}
for _, s2 := range s {
b.WriteString(s2)
}
str = b.String()
}
return
}

View File

@ -112,3 +112,23 @@ func TestRangeSlice(t *testing.T) {
})
}
}
func TestStrJoin(t *testing.T) {
type args struct {
s []string
}
tests := []struct {
name string
args args
wantStr string
}{
{name: "t1", args: args{s: []string{"a", "b", "c"}}, wantStr: "abc"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotStr := StrJoin(tt.args.s...); gotStr != tt.wantStr {
t.Errorf("StrJoin() = %v, want %v", gotStr, tt.wantStr)
}
})
}
}