diff --git a/string.go b/string.go index 1b5220e..47c5a83 100644 --- a/string.go +++ b/string.go @@ -12,10 +12,35 @@ import ( "errors" "git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/util" + "github.com/axgle/mahonia" "io" + "math/rand" "strings" + "time" ) +// StringFromRandom 从随机字符串生成String +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:50 2023/8/11 +func StringFromRandom(length int, sourceCharList string) String { + if length == 0 { + return "" + } + if len(sourceCharList) == 0 { + //字符串为空,默认字符源为如下(去除易混淆的i/l): + sourceCharList = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ" + } + strByte := []byte(sourceCharList) + var genStrByte = make([]byte, 0) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < int(length); i++ { + genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) + } + return String(string(genStrByte)) +} + // String 字符串类型包装 // // Author : go_developer@163.com<白茶清欢> @@ -987,3 +1012,111 @@ func (str String) Md5() StringResult { func (str String) Value() string { return string(str) } + +// GetLetterList 获取字母列表 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:53 2023/8/11 +func (str String) GetLetterList() []string { + return []string{ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", + "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + } +} + +// SnakeCaseToCamel 蛇形字符串转换为驼峰 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 4:58 下午 2021/10/25 +func (str String) SnakeCaseToCamel() string { + if len(str) == 0 { + return "" + } + builder := strings.Builder{} + index := 0 + if str[0] >= 'a' && str[0] <= 'z' { + builder.WriteByte(str[0] - ('a' - 'A')) + index = 1 + } + for i := index; i < len(str); i++ { + if str[i] == '_' && i+1 < len(str) { + if str[i+1] >= 'a' && str[i+1] <= 'z' { + builder.WriteByte(str[i+1] - ('a' - 'A')) + i++ + continue + } + } + // 将ID转为大写 + if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') { + builder.WriteByte('d' - ('a' - 'A')) + continue + } + builder.WriteByte(str[i]) + } + return builder.String() +} + +// Convert 字符串编码转换 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:38 2022/7/9 +func (str String) Convert(sourceCode string, targetCode string) String { + sourceCoder := mahonia.NewDecoder(sourceCode) + sourceResult := sourceCoder.ConvertString(str.Value()) + targetCoder := mahonia.NewDecoder(targetCode) + _, cdata, _ := targetCoder.Translate([]byte(sourceResult), true) + return String(string(cdata)) +} + +// ClearChar 清理指定字符 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:59 2023/8/11 +func (str String) ClearChar(charList ...string) String { + if len(charList) == 0 { + return str + } + formatStr := str.Value() + for _, item := range charList { + formatStr = strings.ReplaceAll(formatStr, item, "") + } + return String(formatStr) +} + +// ReplaceChineseChar 替换常见的中文符号 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:59 2023/4/4 +func (str String) ReplaceChineseChar() String { + charTable := map[string]string{ + "(": "(", + ")": ")", + ":": ":", + ",": ",", + "。": ".", + "【": "]", + "】": "]", + } + return str.ReplaceChar(charTable) +} + +// ReplaceChar 替换指定字符 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:01 2023/8/11 +func (str String) ReplaceChar(charTable map[string]string) String { + if len(charTable) == 0 { + return str + } + formatStr := str.Value() + for k, v := range charTable { + formatStr = strings.ReplaceAll(formatStr, k, v) + } + return String(formatStr) +}