golang 单词大小写驼峰转换下划线格式转换

作者: adm 分类: go 发布时间: 2022-09-04
package word

import (
    "strings"
    "unicode"
)

//单词全部转化为大写
func ToUpper(s string) string {
    return strings.ToUpper(s)
}

//单词全部转化为小写
func ToLower(s string) string {
    return strings.ToLower(s)
}

//下划线单词转为大写驼峰单词
func UderscoreToUpperCamelCase(s string) string {
    s = strings.Replace(s, "_", " ", -1)
    s = strings.Title(s)
    return strings.Replace(s, " ", "", -1)
}

//下划线单词转为小写驼峰单词
func UderscoreToLowerCamelCase(s string) string {
    s = UderscoreToUpperCamelCase(s)
    return string(unicode.ToLower(rune(s[0]))) + s[1:]
    return s
}

//驼峰单词转下划线单词
func CamelCaseToUdnderscore(s string) string {
    var output []rune
    for i, r := range s {
        if i == 0 {
            output = append(output, unicode.ToLower(r))
        } else {
            if unicode.IsUpper(r) {
                output = append(output, '_')
            }

            output = append(output, unicode.ToLower(r))
        }
    }
    return string(output)
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!