common_func
This commit is contained in:
parent
4c27cecde1
commit
d4bc04dbe5
50
common_fun/string_func.go
Normal file
50
common_fun/string_func.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package cf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 是否是手机号
|
||||||
|
func IsMobile(str string) bool {
|
||||||
|
return regexp.MustCompile(`^1[3-9]\d{9}$`).MatchString(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 随机字符串
|
||||||
|
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
|
|
||||||
|
// 获取随机字符串
|
||||||
|
func StrRand(strLen int) string {
|
||||||
|
randBytes := make([]rune, strLen)
|
||||||
|
for i := range randBytes {
|
||||||
|
randBytes[i] = letters[rand.Intn(len(letters))]
|
||||||
|
}
|
||||||
|
return string(randBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 随机字母字符串
|
||||||
|
var charLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||||
|
|
||||||
|
// 随机字母字符串
|
||||||
|
func StrCharRand(strLen int) string {
|
||||||
|
randBytes := make([]rune, strLen)
|
||||||
|
for i := range randBytes {
|
||||||
|
randBytes[i] = charLetters[rand.Intn(len(charLetters))]
|
||||||
|
}
|
||||||
|
return string(randBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 随机数字字符串
|
||||||
|
// 第一位不能是0
|
||||||
|
func StrNumberRand(strLen int) string {
|
||||||
|
numberLetters := []rune("0123456789")
|
||||||
|
randBytes := make([]rune, strLen)
|
||||||
|
for i := range randBytes {
|
||||||
|
if i == 0 {
|
||||||
|
randBytes[i] = letters[rand.Intn(9)]
|
||||||
|
} else {
|
||||||
|
randBytes[i] = letters[rand.Intn(len(numberLetters))]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(randBytes)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user