Files
service-common/utils/string.go

50 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
}