1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package util
- import (
- "math/rand"
- "time"
- "unicode/utf8"
- )
- // StrLen utf8 string length
- func StrLen(s string) int {
- return utf8.RuneCountInString(s)
- }
- // StrMax verify string's max lenth
- func StrMax(s string, max int) bool {
- n := utf8.RuneCountInString(s)
- return n <= max
- }
- // StrMin verify string's min lenth
- func StrMin(s string, min int) bool {
- n := utf8.RuneCountInString(s)
- return n >= min
- }
- // StrRange verify string's Range lenth
- func StrRange(s string, min, max int) bool {
- n := utf8.RuneCountInString(s)
- if n <= max && n >= min {
- return true
- }
- return false
- }
- // Random 指定长度的随机字符串(字母或数字)
- func Random(n int) string {
- str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- n1 := len(bytes)
- for i := 0; i < n; i++ {
- result = append(result, bytes[r.Intn(n1)])
- }
- return string(result)
- }
|