-
Notifications
You must be signed in to change notification settings - Fork 3
/
suffix.go
executable file
·51 lines (41 loc) · 1.11 KB
/
suffix.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
package strKit
import "strings"
// EndWith
/*
PS: 区分大小写.
@param s suffix不为""的情况下,如果s为"",返回值必定为false
@param suffix 如果suffix为"",返回值必定为true
e.g. ""的情况
("", "") => true
("1", "") => true
("", "1") => false
e.g.1 区分大小写
("abc", "abc") => true
("abc", "Abc") => false
*/
var EndWith func(s, suffix string) bool = strings.HasSuffix
var CutSuffix func(s, suffix string) (before string, found bool) = strings.CutSuffix
// RemoveSuffixIfExists 去掉指定的"后缀"(如果存在的话)
/*
PS:
(1) 区分大小写;
(2) 存在多个的话,只会移除最后1个.
*/
var RemoveSuffixIfExists func(s, suffix string) string = strings.TrimSuffix
// AppendIfMissing 如果给定字符串不是以给定的字符串为结尾,则在"尾部"添加结尾字符串(不忽略大小写).
/*
PS: 区分大小写.
@param suffix 后缀
e.g.
("abc", "c")) => "abc"
("abc", "C")) => "abcC"
("abc", "0")) => "abc0"
*/
func AppendIfMissing(str, suffix string) (rst string) {
if EndWith(str, suffix) {
rst = str
} else {
rst = str + suffix
}
return
}