-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
99 lines (90 loc) · 2.18 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package function
import (
"strings"
"unicode"
"unicode/utf8"
)
// StrLengthCutAndSplitJoint 字符串长度截取并拼接处理
func StrLengthCutAndSplitJoint(str string, cutLength int, splitJointStr ...string) string {
if cutLength == 0 {
return str
}
if utf8.RuneCountInString(str) <= cutLength {
return str
}
strRune := []rune(str)
s := string(strRune[0:cutLength])
if len(splitJointStr) > 0 {
s += splitJointStr[0]
}
return s
}
// CheckStringLength 判断字符串长度是否在规定范围内
func CheckStringLength(str string, length int) bool {
if utf8.RuneCountInString(str) <= length {
return true
}
return false
}
// ToBigCamelCase 字符串转大驼峰格式
func ToBigCamelCase(str string, signs ...string) (res string) {
sign := "_"
if len(signs) > 0 {
sign = signs[0]
}
strArr := strings.Split(str, sign)
for _, v := range strArr {
res += strings.ToUpper(string(v[0])) + v[1:]
}
return
}
// ToSmallCamelCase 字符串转小驼峰格式
func ToSmallCamelCase(str string, signs ...string) (res string) {
sign := "_"
if len(signs) > 0 {
sign = signs[0]
}
strArr := strings.Split(str, sign)
for k, v := range strArr {
if k == 0 {
res += strings.ToLower(string(v[0])) + v[1:]
} else {
res += strings.ToUpper(string(v[0])) + v[1:]
}
}
return
}
// UcFirst 仅开头字母大写[将字符串中的第一个字母转换成大写]
func UcFirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// LcFirst 仅开头字母小写[将字符串中的第一个字母转换成小写]
func LcFirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
// UcWords 所有首字母大写[将字符串中每个单词的首字母转换成大写]
func UcWords(str string) (res string) {
arr := strings.Split(str, " ")
sign := ""
for _, v := range arr {
res += sign + UcFirst(v)
sign = " "
}
return
}
// LcWords 所有首字母小写[将字符串中每个单词的首字母转换成小写]
func LcWords(str string) (res string) {
arr := strings.Split(str, " ")
sign := ""
for _, v := range arr {
res += sign + LcFirst(v)
sign = " "
}
return
}