-
Notifications
You must be signed in to change notification settings - Fork 3
/
prefix.go
executable file
·73 lines (59 loc) · 1.56 KB
/
prefix.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 strKit
import "strings"
// StartWith
/*
PS: 区分大小写.
@param s prefix不为""的情况下,如果s为"",返回值必定为false
@param prefix 如果prefix为"",返回值必定为true
e.g. ""的情况
("", "") => true
("1", "") => true
("", "1") => false
e.g.1 区分大小写
("abc", "abc") => true
("abc", "Abc") => false
*/
var StartWith func(s, prefix string) bool = strings.HasPrefix
// CutPrefix
/*
e.g.
fmt.Println(strKit.CutPrefix("abcd", "ab")) // cd true
fmt.Println(strKit.CutPrefix("abcd", "e")) // abcd false
*/
var CutPrefix func(s, prefix string) (after string, found bool) = strings.CutPrefix
// RemovePrefixIfExists 去掉指定的"前缀"(如果存在的话)
/*
PS:
(1) 区分大小写;
(2) 存在多个的话,只会移除第1个.
@param s 如果为"",返回""
@param prefix 如果为"",返回传参s
e.g.
fmt.Println(strKit.RemovePrefixIfExists("abcd", "ab")) // cd
fmt.Println(strKit.RemovePrefixIfExists("abcd", "e")) // abcd
e.g.1 ""的情况
("", "") => ""
("1", "") => "1"
("", "1") => ""
e.g.1 区分大小写
("abcd", "abcd") => ""
("abcd", "Abcd") => "abcd"
*/
var RemovePrefixIfExists func(s, prefix string) string = strings.TrimPrefix
// PrependIfMissing 如果给定字符串不是以给定的字符串为开头,则在"首部"添加 起始字符串.
/*
PS: 区分大小写.
@param prefix 前缀
e.g.
("abc", "a") => "abc"
("abc", "A")) => "Aabc"
("abc", "0") => "0abc"
*/
func PrependIfMissing(str, prefix string) (rst string) {
if StartWith(str, prefix) {
rst = str
} else {
rst = prefix + str
}
return
}