github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/stringutils/stringutils.go (about) 1 package stringutils 2 3 import ( 4 "regexp" 5 "strings" 6 "unicode" 7 ) 8 9 // IsEmpty asserts s is empty 10 func IsEmpty(s string) bool { 11 return strings.TrimSpace(s) == "" 12 } 13 14 // IsNotEmpty asserts s is not empty 15 func IsNotEmpty(s string) bool { 16 return !IsEmpty(s) 17 } 18 19 // ContainsI assert s contains substr ignore case 20 func ContainsI(s string, substr string) bool { 21 re := regexp.MustCompile(`(?i)` + substr) 22 return re.MatchString(s) 23 } 24 25 // HasPrefixI assert s has prefix prefix ignore case 26 func HasPrefixI(s, prefix string) bool { 27 re := regexp.MustCompile(`(?i)^` + prefix) 28 return re.MatchString(s) 29 } 30 31 func ToTitle(s string) string { 32 str := []rune(s) 33 return strings.ToUpper(string(str[0])) + string(str[1:]) 34 } 35 36 var symbolre = regexp.MustCompile("[。?!,、;:“”‘’()《》【】~!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\\s]") 37 38 func ToCamel(s string) string { 39 parts := symbolre.Split(s, -1) 40 var convertedParts []string 41 for _, v := range parts { 42 if IsNotEmpty(v) { 43 convertedParts = append(convertedParts, ToTitle(v)) 44 } 45 } 46 ret := strings.Join(convertedParts, "") 47 if !unicode.IsUpper(rune(ret[0])) { 48 ret = "A" + ret 49 } 50 return ret 51 }