feat 初始化
This commit is contained in:
49
utils/string.go
Normal file
49
utils/string.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// GetMD5Encode 获取Md5编码
|
||||
func GetMD5Encode(data string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(data))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
const (
|
||||
StringChars1 = "0123456789"
|
||||
StringChars2 = "abcdefghijklmnopqrstuvwxyz"
|
||||
StringChars3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
StringChars4 = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
StringChars5 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
StringChars6 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
StringChars7 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
)
|
||||
|
||||
// RandomString 获取随机字符串
|
||||
func RandomString(chars string, length int8) string {
|
||||
result := make([]byte, length)
|
||||
for i := int8(0); i < length; i++ {
|
||||
result[i] = chars[rand.Intn(len(chars))]
|
||||
if i == 0 && result[i] == '0' {
|
||||
i--
|
||||
}
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
// GetDisplayWidth 获取字符串长度
|
||||
func GetDisplayWidth(text string) int {
|
||||
width := 0
|
||||
for _, r := range text {
|
||||
if r <= 0x7F {
|
||||
width += 1 // ASCII,半角
|
||||
} else {
|
||||
width += 2 // CJK、日文假名等,全角
|
||||
}
|
||||
}
|
||||
return width
|
||||
}
|
||||
Reference in New Issue
Block a user