增加蛇形转驼峰方法

This commit is contained in:
2021-10-25 18:06:26 +08:00
parent bd32998c76
commit 5a7a4099e2
3 changed files with 29 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"crypto/md5"
"encoding/hex"
"math/rand"
"strings"
"time"
)
@ -46,3 +47,28 @@ func Md5(str string) string {
_, _ = h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// SnakeCaseToCamel 蛇形字符串转换为驼峰
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:58 下午 2021/10/25
func SnakeCaseToCamel(str string) string {
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
}
}
builder.WriteByte(str[i])
}
return builder.String()
}