-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
365 lines (321 loc) · 7.04 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package util
/*
string utils
@author Tony Tian
@date 2018-03-17
@version 1.0.0
*/
import (
"fmt"
"strconv"
"strings"
)
/*
String struct.
Usage:
str1 := NewString("13990521234")
str2 := NewString("14")
str3 := str1.Substring(0, 2).Append(str2).AppendString("520")
println(str1.ToString())
println(str2.ToString())
println(str3.ToString())
print:
13990521234
14
1314520
*/
type String struct {
value string
}
func NewString(s string) *String {
var str String
str.value = s
return &str
}
func NewStringInt(i int) *String {
var str String
str.value = strconv.Itoa(i)
return &str
}
func NewStringInt64(i int64) *String {
var str String
str.value = strconv.FormatInt(i, 10)
return &str
}
func NewStringFloat64(f float64) *String {
var str String
str.value = strconv.FormatFloat(f, 'f', -1, 64)
return &str
}
func NewStringBool(b bool) *String {
var str String
str.value = strconv.FormatBool(b)
return &str
}
func NewStringAny(f interface{}) *String {
var str *String
switch v := f.(type) {
case string:
str = NewString(v)
case int:
str = NewStringInt(v)
case int8:
str = NewStringInt64(int64(v))
case int16:
str = NewStringInt64(int64(v))
case int32:
str = NewStringInt64(int64(v))
case int64:
str = NewStringInt64(int64(v))
case float32:
str = NewStringFloat64(float64(v))
case float64:
str = NewStringFloat64(v)
case bool:
str = NewStringBool(v)
case uint:
str = &String{value: (strconv.FormatUint(uint64(v), 10))}
case uint8:
str = &String{value: (strconv.FormatUint(uint64(v), 10))}
case uint16:
str = &String{value: (strconv.FormatUint(uint64(v), 10))}
case uint32:
str = &String{value: (strconv.FormatUint(uint64(v), 10))}
case uint64:
str = &String{value: (strconv.FormatUint(v, 10))}
case fmt.Stringer:
str = NewString(v.String())
}
return str
}
func (str *String) ToString() string {
return str.value
}
func (str *String) String() string {
return str.value
}
func (str *String) Clear() *String {
var newStr string
str.value = newStr
return str
}
/*
"123" -> 3
*/
func (str *String) Len() int {
return len(str.value)
}
/*
"123xxxbbb5990" -> "123x" = true
*/
func (str *String) StartsWith(s string) bool {
return str.SubstringEnd(len(s)).ToString() == s
}
/*
"123xxxbbb5990" -> "5990" = true
*/
func (str *String) EndsWith(s string) bool {
return str.SubstringBegin(str.Len()-len(s)).ToString() == s
}
/*
" 123 " -> "123"
" 1 23 " -> "1 23"
*/
func (str *String) Trim() *String {
return NewString(strings.Trim(str.value, SPACE))
}
/*
"%111%abc%987%" -> ("%", "$") -> "$111$abc$987$"
*/
func (str *String) Replace(old, new string) *String {
return NewString(strings.Replace(str.value, old, new, -1))
}
/*
"abc" -> 1 -> "ac"
*/
func (str *String) Remove(index int) *String {
strTmp := NewStringBuilder().Append(str.SubstringEnd(index).ToString()).Append(str.SubstringBegin(index + 1).ToString()).ToString()
return NewString(strTmp)
}
/*
"abc" -> "ab"
*/
func (str *String) RemoveLast() *String {
return str.Substring(0, str.Len()-1)
}
/*
If a string contains a string, return true, and ignore case.
eg: "strings insert chars"
chars = "insert" -> true
chars = "Insert" -> true
chars = "key" -> false
*/
func (str *String) ContainsIgnoreCase(chars string) bool {
return str.ToLower().Contains(strings.ToLower(chars))
}
/*
If a string contains a string, return true
eg: "strings insert chars"
chars = "insert" -> true
chars = "Insert" -> false
chars = "key" -> false
*/
func (str *String) Contains(chars string) bool {
return strings.Contains(str.value, chars)
}
/*
abcdef -> b = 1
*/
func (str *String) LastIndex(chars string) int {
return strings.LastIndex(str.value, chars)
}
/*
abcdef -> e = 4
*/
func (str *String) Index(chars string) int {
return strings.Index(str.value, chars)
}
/*
"12345" -> 12345
*/
func (str *String) ToInt() (int, error) {
return strconv.Atoi(str.value)
}
func (str *String) ToInt64() (int64, error) {
return strconv.ParseInt(str.value, 10, 64)
}
func (str *String) ToFloat() (float64, error) {
return strconv.ParseFloat(str.value, 64)
}
func (str *String) ToIntV() int {
v, err := str.ToInt()
if err != nil {
panic(err)
}
return v
}
func (str *String) ToInt64V() int64 {
v, err := str.ToInt64()
if err != nil {
panic(err)
}
return v
}
func (str *String) ToFloatV() float64 {
v, err := str.ToFloat()
if err != nil {
panic(err)
}
return v
}
func (str *String) ToBoolV() bool {
v, err := strconv.ParseBool(str.ToString())
if err != nil {
panic(err)
}
return v
}
/*
str := NewString("abcde")
str.Substring(0, 2)
return: "ab"
*/
func (str *String) Substring(beginIndex, endIndex int) *String {
return NewString(str.value[beginIndex:endIndex])
}
/*
str := NewString("abcde")
str.SubstringBegin(2)
return: "cde"
*/
func (str *String) SubstringBegin(beginIndex int) *String {
return str.Substring(beginIndex, str.Len())
}
/*
str := NewString("abcde")
str.SubstringEnd(3)
return: "abc"
*/
func (str *String) SubstringEnd(endIndex int) *String {
return str.Substring(0, endIndex)
}
func (str *String) Append(arg *String) *String {
strTmp := NewStringBuilder().Append(str.value).Append(arg.ToString()).ToString()
return NewString(strTmp)
}
func (str *String) AppendString(arg string) *String {
strTmp := NewStringBuilder().Append(str.value).Append(arg).ToString()
return NewString(strTmp)
}
func (str *String) AppendInt(i int) *String {
strTmp := str.value + strconv.Itoa(i)
return NewString(strTmp)
}
func (str *String) AppendInt64(i int64) *String {
strTmp := str.value + strconv.FormatInt(i, 10)
return NewString(strTmp)
}
func (str *String) AppendFloat64(f float64) *String {
strTmp := str.value + strconv.FormatFloat(f, 'E', -1, 64)
return NewString(strTmp)
}
/*
"460364431014955c2483ec91230e5435" -> [4 6 0 3 6 4 4 3 1 0 1 4 9 5 5 c 2 4 8 3 e c 9 1 2 3 0 e 5 4 3 5]
*/
func (str *String) ToArray() []string {
return strings.Split(str.value, "")
}
/*
"aaa" -> "AAA"
*/
func (str *String) ToLower() *String {
return NewString(strings.ToLower(str.value))
}
/*
"BBB" -> "bbb"
*/
func (str *String) ToUpper() *String {
return NewString(strings.ToUpper(str.value))
}
/*
first = false: "aaa_bbb_ccc" -> "aaaBbbCcc"
first = true: "aaa_bbb_ccc" -> "AaaBbbCcc"
*/
func FirstCaseToUpper(str string, first bool) string {
temp := strings.Split(str, "_")
var upperStr string
for y := 0; y < len(temp); y++ {
vv := []rune(temp[y])
if y == 0 && !first {
continue
}
for i := 0; i < len(vv); i++ {
if i == 0 {
vv[i] -= 32
upperStr += string(vv[i])
} else {
upperStr += string(vv[i])
}
}
}
if first {
return upperStr
} else {
return temp[0] + upperStr
}
}
/*
[9 9 8 4 2 9 1 7 - a 5 4 b - 3 3 1 6 - c d f 3 - 8 7 d 9 f b 5 7] -> "99842917-a54b-3316-cdf3-87d9fb57"
*/
func ArrayToString(arrays []string) string {
return strings.Join(arrays, "")
}
//json化过的字符串
type StringJson string
func (this StringJson) MarshalJSON() ([]byte, error) {
return []byte(this), nil
}
func (this *StringJson) UnmarshalJSON(v []byte) error {
*this = StringJson(v)
return nil
}