-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
54 lines (43 loc) · 875 Bytes
/
strings.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
package utils
import "strings"
func Substr(s string, start int, length *int) string {
if start < 0 {
start = len(s) + start
}
if start < 0 || start >= len(s) {
return ""
}
var end int
if length == nil {
end = len(s)
} else {
end = start + *length
}
if end > len(s) {
end = len(s)
}
return s[start:end]
}
func Substring(s string, start int, length *int) string {
return Substr(s, start, length)
}
func Strpos(stringStr string, needle string) int {
return strings.LastIndex(stringStr, needle)
}
func StringPosition(stringStr string, needle string) int {
return Strpos(stringStr, needle)
}
func Strncmp(s1, s2 string, n int) int {
sub1 := s1
sub2 := s2
if len(s1) > n {
sub1 = s1[:n]
}
if len(s2) > n {
sub2 = s2[:n]
}
return strings.Compare(sub1, sub2)
}
func StringCompare(s1, s2 string, n int) int {
return Strncmp(s1, s2, n)
}