95 lines
2.0 KiB
Go
Executable File
95 lines
2.0 KiB
Go
Executable File
package helper
|
||
|
||
import (
|
||
"math/rand"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
"unicode"
|
||
)
|
||
|
||
func StringToInt64(s string) int64 {
|
||
i, _ := strconv.ParseInt(s, 10, 64)
|
||
return i
|
||
}
|
||
|
||
func StringToInt32(s string) int32 {
|
||
i, _ := strconv.ParseInt(s, 10, 64)
|
||
return int32(i)
|
||
}
|
||
|
||
// StringToInt32WithError 将字符串转换为int32
|
||
func StringToInt32WithError(s string) (int32, error) {
|
||
i, err := strconv.ParseInt(s, 10, 64)
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
return int32(i), nil
|
||
}
|
||
|
||
// StringToAsterisk 替换密钥中间字符为 *
|
||
func StringToAsterisk(value string) string {
|
||
length := len(value)
|
||
if length == 0 {
|
||
return ""
|
||
}
|
||
asterisk := "*"
|
||
if length < 4 {
|
||
asterisk = strings.Repeat(asterisk, length)
|
||
return strings.Replace(value, value, asterisk, 1)
|
||
}
|
||
|
||
offset := length / 4
|
||
asterisk = strings.Repeat(asterisk, length-offset*2)
|
||
return value[:offset] + asterisk + value[length-offset:]
|
||
}
|
||
|
||
func StrSuffixNumSort(in []string) []string {
|
||
sort.Slice(in, func(i, j int) bool {
|
||
return getSuffixNumber(in[i]) < getSuffixNumber(in[j])
|
||
})
|
||
var result []string
|
||
for _, str := range in {
|
||
result = append(result, str)
|
||
}
|
||
|
||
return result
|
||
}
|
||
|
||
// 获取字符串中最后面的后缀数字
|
||
func getSuffixNumber(s string) int {
|
||
parts := strings.Split(s, "_")
|
||
lastPart := parts[len(parts)-1] // 获取最后一个部分
|
||
numStr := ""
|
||
for i := len(lastPart) - 1; i >= 0; i-- {
|
||
if _, err := strconv.Atoi(string(lastPart[i])); err == nil {
|
||
numStr = string(lastPart[i]) + numStr
|
||
} else {
|
||
break
|
||
}
|
||
}
|
||
num, _ := strconv.Atoi(numStr)
|
||
return num
|
||
}
|
||
|
||
func GetRandomStr(length int) string {
|
||
rand.Seed(time.Now().UnixNano())
|
||
// 去掉容易搞错的字符,例如0,1,l,L,0,O,o,I,i
|
||
const charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ123456789"
|
||
password := make([]byte, length)
|
||
for i := range password {
|
||
password[i] = charset[rand.Intn(len(charset))]
|
||
}
|
||
return string(password)
|
||
}
|
||
|
||
func ToTitleFirst(s string) string {
|
||
if s == "" {
|
||
return s
|
||
}
|
||
runes := []rune(s)
|
||
runes[0] = unicode.ToUpper(runes[0])
|
||
return string(runes)
|
||
}
|