-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
122 lines (109 loc) · 2.92 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package function
import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// StringLengthCutAndSplitJoint 字符串长度截取并拼接处理
func StringLengthCutAndSplitJoint(str string, cutLength int, splitJointStr ...string) string {
if cutLength == 0 {
return str
}
if StringLength(str) <= cutLength {
return str
}
strRune := []rune(str)
s := string(strRune[0:cutLength])
if len(splitJointStr) > 0 {
s += splitJointStr[0]
}
return s
}
// StringLength 获取字符串长度[包含中文字符串,一个中文字符串的长度为1]
func StringLength(str string) int {
return utf8.RuneCountInString(str)
}
// CheckStringLength 判断字符串长度是否在规定范围内
func CheckStringLength(str string, length int) bool {
if StringLength(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
}
// Int64LengthPadding 数字长度不够左侧填补0并返回字符串
func Int64LengthPadding(i64 int64, length int) string {
return fmt.Sprintf("%0"+strconv.Itoa(length)+"d", i64)
}
// StringLengthPadding 字符串长度不够左侧填补0
func StringLengthPadding(str string, length int) string {
return fmt.Sprintf("%0"+strconv.Itoa(length)+"s", str)
}
// GetFirstAndEndString 获取第一个和最后一个字符串[不区分中英文]
func GetFirstAndEndString(str string) (string, string) {
slice := []rune(str)
return string(slice[0]), string(slice[len(slice)-1])
}