add: word transfer

This commit is contained in:
vilet.yy 2021-06-10 14:41:41 +08:00
parent 619e954daa
commit 18549b22db
1 changed files with 51 additions and 0 deletions

51
word/word.go Normal file
View File

@ -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)
}