-
Notifications
You must be signed in to change notification settings - Fork 8
/
str_format.go
73 lines (67 loc) · 1.7 KB
/
str_format.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
package filter
import (
"strings"
)
// 字母转为大写
func (self *Str) ToUpper() StrType {
if self.err != nil || self.currentValue == "" {
return self
}
self.currentValue = strings.ToUpper(self.currentValue)
return self
}
// 字母转为小写
func (self *Str) ToLower() StrType {
if self.err != nil || self.currentValue == "" {
return self
}
self.currentValue = strings.ToLower(self.currentValue)
return self
}
// SnakeCaseToCamelCase 蛇形转驼峰: hello_world => helloWorld
func (self *Str) SnakeCaseToCamelCase() StrType {
if self.err != nil || self.currentValue == "" {
return self
}
slice := strings.Split(self.currentValue, "_")
for i := range slice {
if i > 0 {
slice[i] = strings.Title(slice[i])
}
}
self.currentValue = strings.Join(slice, "")
return self
}
// SnakeCaseToPascalCase 蛇形转帕斯卡: hello_world => HelloWorld
func (self *Str) SnakeCaseToPascalCase() StrType {
if self.err != nil || self.currentValue == "" {
return self
}
slice := strings.Split(self.currentValue, "_")
for i := range slice {
slice[i] = strings.Title(slice[i])
}
self.currentValue = strings.Join(slice, "")
return self
}
// CamelCaseToSnakeCase 驼峰(含帕斯卡)转蛇形 helloWorld/HelloWorld => hello_world
func (self *Str) CamelCaseToSnakeCase() StrType {
if self.err != nil || self.currentValue == "" {
return self
}
strLen := len(self.currentValue)
result := make([]byte, 0, strLen*2)
j := false
for i := 0; i < strLen; i++ {
char := self.currentValue[i]
if i > 0 && char >= 'A' && char <= 'Z' && j {
result = append(result, '_')
}
if char != '_' {
j = true
}
result = append(result, char)
}
self.currentValue = strings.ToLower(string(result))
return self
}