Files
go-utils/stringcase/snake_case.go
2025-06-23 23:26:59 +08:00

51 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package stringcase
import (
"strings"
)
// ToSnakeCase 把字符转换为 蛇形命名法snake_case
func ToSnakeCase(input string) string {
return SnakeCase(input)
}
func SnakeCase(s string) string {
return delimiterCase(s, '_', false)
}
func UpperSnakeCase(s string) string {
return delimiterCase(s, '_', true)
}
func delimiterCase(input string, delimiter rune, upperCase bool) string {
input = strings.TrimSpace(input)
if input == "" {
return input
}
// 使用 Split 分割字符串
words := Split(input)
filteredWords := make([]string, 0, len(words))
for _, word := range words {
if strings.TrimSpace(word) != "" {
filteredWords = append(filteredWords, word)
}
}
adjustCase := toLower
if upperCase {
adjustCase = toUpper
}
for i, word := range filteredWords {
runes := []rune(word)
for j := 0; j < len(runes); j++ {
runes[j] = adjustCase(runes[j])
}
filteredWords[i] = string(runes)
}
// 使用分隔符连接结果
return strings.Join(filteredWords, string(delimiter))
}