-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
165 lines (152 loc) · 3.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package kitgo
import (
"errors"
"regexp"
"strings"
)
//-------------------------------------
//
//
//
//-------------------------------------
func StringIgnoreEquals(src string, dest string) bool {
src = strings.ToLower(src)
dest = strings.ToLower(dest)
return src == dest
}
//-------------------------------------
//
// 正则切割
//
//-------------------------------------
func StringSplitByRegexp(src string, reg string) []string {
pattern := regexp.MustCompile(reg)
indexes := pattern.FindAllStringIndex(src, -1)
laststart := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = src[laststart:element[0]]
laststart = element[1]
}
result[len(indexes)] = src[laststart:len(src)]
return result
}
//-------------------------------------
//
// 首字母小写
//
//-------------------------------------
func StringFirstLetterLower(src string) string {
return strings.ToLower(src[:1]) + src[1:]
}
//-------------------------------------
//
// 首字母大写
//
//-------------------------------------
func StringFirstLetterUpper(src string) string {
return strings.ToUpper(src[:1]) + src[1:]
}
//-------------------------------------
//
// 取首尾空格
//
//-------------------------------------
func StringTrim(src string) string {
return strings.TrimSpace(src)
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringBetween(src string, start string, end string) (string, bool) {
sI := strings.Index(src, start)
if sI >= 0 {
src = src[sI+len(start):]
eI := strings.Index(src, end)
if eI > 0 {
return src[:eI], true
} else {
//Log(src, "开始位置[", end, "]没有找到")
return src, false
}
} else {
//Log(src, "开始位置[", start, "]没有找到")
return "", false
}
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringStartWith(src string, s string) bool {
return strings.Index(src, s) == 0
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringAfter(src string, start string) (string, bool) {
sI := strings.Index(src, start)
if sI >= 0 {
return src[sI+len(start):], true
} else {
return "", false
}
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringBefore(src string, start string) (string, bool) {
sI := strings.Index(src, start)
if sI >= 1 {
return src[:sI], true
} else {
return "", false
}
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringMatch(src string, p string, group int) (string, error) {
pattern, err := regexp.Compile(p)
if err != nil {
return "", err
} else {
r := pattern.FindStringSubmatch(src)
if len(r) > group {
return r[group], nil
} else {
return "", errors.New("匹配失败")
}
}
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringReplace(src string, p string, r string) string {
rp := regexp.MustCompile(p)
return rp.ReplaceAllString(src, r)
}
//-------------------------------------
//
//
//
//-------------------------------------
func StringLeftPad(src string, length int, pad string) string {
srcLen := len(src)
offset := length - srcLen
for i := 0; i < offset; i++ {
src = pad + src
}
return src
}