From 18549b22db3c8c6e6c2f498dfdeb2ebc7a2c833d Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Thu, 10 Jun 2021 14:41:41 +0800 Subject: [PATCH] add: word transfer --- word/word.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 word/word.go diff --git a/word/word.go b/word/word.go new file mode 100644 index 0000000..20d80c7 --- /dev/null +++ b/word/word.go @@ -0,0 +1,51 @@ +/* + * @Date: 2021-06-10 14:33:29 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-10 14:40:55 + * @FilePath: /yolk/word/word.go + */ +package word + +import ( + "strings" + "unicode" +) + +// 全部转为大写 +func ToUpper(s string) string { + return strings.ToUpper(s) +} + +// 全部转为小写 +func ToLower(s string) string { + return strings.ToLower(s) +} + +// 下划线转大写驼峰 +func UnderscoreToUpperCamelCase(s string) string { + s = strings.Replace(s, "_", " ", -1) + s = strings.Title(s) + return strings.Replace(s, " ", "", -1) +} + +// 下划线转小写驼峰 +func UnderscoreToLowerCamelCase(s string) string { + s = UnderscoreToUpperCamelCase(s) + return string(unicode.ToLower(rune(s[0]))) + s[1:] +} + +// 驼峰转下划线 +func CamelCaseToUnderscore(s string) string { + var output []rune + for i, r := range s { + if i == 0 { + output = append(output, unicode.ToLower(r)) + continue + } + if unicode.IsUpper(r) { + output = append(output, '_') + } + output = append(output, unicode.ToLower(r)) + } + return string(output) +}