package util import ( "strconv" "strings" "unicode" ) //判断是否是数字字符串 func IsDigit(str string) bool { for _, x := range []rune(str) { if !unicode.IsDigit(x) { return false } } return true } func Atoi(i string) int { if i == "" { return 0 } j, err := strconv.Atoi(i) if err != nil { return 0 } else { return j } } func StringToInt(s interface{}) int { switch i := s.(type) { case string: return Atoi(i) case float32: return int(i) case float64: return int(i) case int: return i case uint: return int(i) } return 0 } /** * 驼峰转蛇形 snake string * @description XxYy to xx_yy , XxYY to xx_y_y * @date 2020/7/30 * @param s 需要转换的字符串 * @return string **/ func SnakeString(s string) string { data := make([]byte, 0, len(s)*2) j := false num := len(s) for i := 0; i < num; i++ { d := s[i] // or通过ASCII码进行大小写的转化 // 65-90(A-Z),97-122(a-z) //判断如果字母为大写的A-Z就在前面拼接一个_ if i > 0 && d >= 'A' && d <= 'Z' && j { data = append(data, '_') } if d != '_' { j = true } data = append(data, d) } //ToLower把大写字母统一转小写 return strings.ToLower(string(data[:])) } /** * 蛇形转驼峰 * @description xx_yy to XxYx xx_y_y to XxYY * @date 2020/7/30 * @param s要转换的字符串 * @return string **/ func CamelString(s string) string { data := make([]byte, 0, len(s)) j := false k := false num := len(s) - 1 for i := 0; i <= num; i++ { d := s[i] if k == false && d >= 'A' && d <= 'Z' { k = true } if d >= 'a' && d <= 'z' && (j || k == false) { d = d - 32 j = false k = true } if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' { j = true continue } data = append(data, d) } return string(data[:]) }