-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
232 lines (206 loc) · 4.76 KB
/
string.go
File metadata and controls
232 lines (206 loc) · 4.76 KB
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
/*
@Time : 2022/5/20 13:32
@Author : LiuKun
@File : tools
@Software: GoLand
@Description:
*/
package util
import (
"fmt"
"github.com/mozillazg/go-pinyin"
"regexp"
"sort"
"strings"
"time"
"unicode"
)
// RemoveNonNumeric 去除非数字部分
//输入"abc123def456", 输出 "123456"
func RemoveNonNumeric(str string) string {
regex := regexp.MustCompile("[^0-9]+")
result := regex.ReplaceAllString(str, "")
return result
}
// GetDateFromString 从字符串获取日期,返回年月日
func GetDateFromString(s string) (int, int, int) {
year, month, day := 0, 0, 0
temp := s
if strings.Contains(temp, "年") {
ss := strings.Split(temp, "年")
year = GetIntFromV(ss[0])
if len(ss) > 1 {
temp = ss[1]
}
}
if strings.Contains(temp, "月") {
ss := strings.Split(temp, "月")
month = GetIntFromV(ss[0])
if len(ss) > 1 {
temp = ss[1]
}
}
if strings.Contains(temp, "日") {
ss := strings.Split(temp, "日")
day = GetIntFromV(ss[0])
}
return year, month, day
}
// DurationDes 时长描述
func DurationDes(d time.Duration) string {
s := ""
hour := int64(d) / int64(time.Hour)
if hour > 0 {
s += fmt.Sprintf("%d小时", hour)
}
minute := (int64(d) % int64(time.Hour)) / int64(time.Minute)
if minute > 0 {
s += fmt.Sprintf("%d分", minute)
}
second := float64((int64(d)%int64(time.Hour))%int64(time.Minute)) / float64(time.Second)
if second > 0 {
s += fmt.Sprintf("%.3f秒", second)
}
return s
}
// LetterMarkSecondLastChinese 字母标记倒数第二个汉字
func LetterMarkSecondLastChinese(str string) string {
indexes := make([]int, 0)
rt := []rune(str)
for i, v := range rt {
if unicode.Is(unicode.Han, v) {
indexes = append(indexes, i)
}
}
if len(indexes) >= 2 {
oStr := string(rt[indexes[len(indexes)-2]])
pins := pinyin.Pinyin(oStr, pinyin.NewArgs())
if len(pins) > 0 && len(pins[0]) > 0 {
p := pins[0][0]
if len(p) > 0 {
rStr := strings.ToUpper(p[:1])
count := strings.Count(str, oStr)
if count < 2 {
//只出现一个
return strings.ReplaceAll(str, oStr, rStr)
}
//步步高、拼多多、新论新材
replaceCount := count - 1
lastStr := string(rt[indexes[len(indexes)-1]])
if lastStr == oStr {
replaceCount -= 1
}
newStr := strings.Replace(str, oStr, "ShouldReplace", replaceCount)
newStr = strings.Replace(newStr, oStr, rStr, 1)
newStr = strings.ReplaceAll(newStr, "ShouldReplace", oStr)
return newStr
}
}
}
return str
}
// GetMapValueSlice 获取Value值切片, 不存在的Key为空字符串
func GetMapValueSlice(keys []string, m map[string]string) []string {
vs := make([]string, 0)
for _, k := range keys {
v, ok := m[k]
if ok {
vs = append(vs, v)
} else {
vs = append(vs, "")
}
}
return vs
}
// RemoveDuplicatedString 去除重复的字符串
func RemoveDuplicatedString(slice []string) []string {
ss := make([]string, 0)
m := make(map[string]bool)
for _, s := range slice {
_, ok := m[s]
if !ok {
m[s] = true
ss = append(ss, s)
}
}
return ss
}
// MatchKeys 匹配关键字 ([index], map[index]key)
func MatchKeys(text string, keys []string) ([]int, map[int]string) {
containsMaps := make(map[int]string)
for _, key := range keys {
i := strings.Index(text, key)
if i >= 0 {
k, ok := containsMaps[i]
if ok {
if len(k) > len(key) {
//优先匹配长的,规避“东方通信” 被 “东方通” 和 “东方通讯同时匹配”
containsMaps[i] = k
}
} else {
containsMaps[i] = key
}
}
}
indexes := make([]int, 0)
for k := range containsMaps {
indexes = append(indexes, k)
}
sort.Ints(indexes)
return indexes, containsMaps
}
// FloatSliceToString 浮点数数组转字符串
func FloatSliceToString(fs []float64, split string) string {
ns := ""
for i, f := range fs {
ns += fmt.Sprintf("%.2f", f)
if i < len(fs)-1 {
ns += split
}
}
return ns
}
// FloatPercentSliceToString 浮点百分比数组转字符串
func FloatPercentSliceToString(fs []float64, split string) string {
ns := ""
for i, f := range fs {
ns += fmt.Sprintf("%.2f%%", f*100)
if i < len(fs)-1 {
ns += split
}
}
return ns
}
// IntSliceToString 整数数组转字符串
func IntSliceToString(is []int, split string) string {
ns := ""
for i, f := range is {
ns += fmt.Sprintf("%d", f)
if i < len(is)-1 {
ns += split
}
}
return ns
}
// StringSliceToString 字符串数组转字符串
func StringSliceToString(ss []string, split string) string {
ns := ""
for i, s := range ss {
ns += s
if i < len(ss)-1 {
ns += split
}
}
return ns
}
// InterfaceSliceToString object数组转字符串
func InterfaceSliceToString(ss []interface{}, split string) string {
ns := ""
for i, s := range ss {
ns += GetStringFromV(s)
if i < len(ss)-1 {
ns += split
}
}
return ns
}