goext/lang/langext/string.go

64 lines
1.1 KiB
Go
Raw Normal View History

2022-10-27 16:00:57 +02:00
package langext
import (
"fmt"
"strings"
)
func StrLimit(val string, maxlen int, suffix string) string {
if len(val) > maxlen {
return val[0:maxlen-len(suffix)] + suffix
}
return val
}
func StrSplit(val string, sep string, allowEmpty bool) []string {
var arr []string
for _, k := range strings.Split(val, sep) {
if allowEmpty || k != "" {
arr = append(arr, k)
}
}
return arr
}
func StrPadRight(str string, pad string, padlen int) string {
if pad == "" {
pad = " "
}
if len(str) >= padlen {
return str
}
return str + strings.Repeat(pad, padlen-len(str))[0:(padlen-len(str))]
}
func StrPadLeft(str string, pad string, padlen int) string {
if pad == "" {
pad = " "
}
if len(str) >= padlen {
return str
}
return strings.Repeat(pad, padlen-len(str))[0:(padlen-len(str))] + str
}
func DeRefStringer(v fmt.Stringer) *string {
if v == nil {
return nil
} else {
return Ptr(v.String())
}
}
func ConvertStringerArray[T fmt.Stringer](inarr []T) []string {
result := make([]string, 0, len(inarr))
for _, v := range inarr {
result = append(result, v.String())
}
return result
}