Files
apinto/utils/string.go
2023-02-09 14:35:49 +08:00

78 lines
1.4 KiB
Go

package utils
import (
"strconv"
"strings"
)
// TrimPrefixAll 删除所有前缀
func TrimPrefixAll(s string, r string) string {
for strings.HasPrefix(s, r) {
s = strings.TrimPrefix(s, r)
}
return s
}
// TrimSuffixAll 删除所有后缀
func TrimSuffixAll(s string, r string) string {
for strings.HasSuffix(s, r) {
s = strings.TrimSuffix(s, r)
}
return s
}
func InterfaceToString(value interface{}) string {
var key string
if value == nil {
return key
}
switch value.(type) {
case float64:
ft := value.(float64)
key = strconv.FormatFloat(ft, 'f', -1, 64)
case float32:
ft := value.(float32)
key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
case int:
it := value.(int)
key = strconv.Itoa(it)
case uint:
it := value.(uint)
key = strconv.Itoa(int(it))
case int8:
it := value.(int8)
key = strconv.Itoa(int(it))
case uint8:
it := value.(uint8)
key = strconv.Itoa(int(it))
case int16:
it := value.(int16)
key = strconv.Itoa(int(it))
case uint16:
it := value.(uint16)
key = strconv.Itoa(int(it))
case int32:
it := value.(int32)
key = strconv.Itoa(int(it))
case uint32:
it := value.(uint32)
key = strconv.Itoa(int(it))
case int64:
it := value.(int64)
key = strconv.FormatInt(it, 10)
case uint64:
it := value.(uint64)
key = strconv.FormatUint(it, 10)
case string:
key = value.(string)
case []byte:
key = string(value.([]byte))
default:
return ""
}
return key
}