Commit dd72ba20dc678c7adba4abf00959e778489f3ff9
1 parent
836cccf887
Exists in
master
CharAt
Showing
1 changed file
with
20 additions
and
0 deletions
Show diff stats
string_util.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | "strings" | 5 | "strings" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | // IsWrappedWith ... | 8 | // IsWrappedWith ... |
9 | func IsWrappedWith(src, begin, end string) bool { | 9 | func IsWrappedWith(src, begin, end string) bool { |
10 | return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) | 10 | return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) |
11 | } | 11 | } |
12 | 12 | ||
13 | // ParseInt64Arr ... | 13 | // ParseInt64Arr ... |
14 | func ParseInt64Arr(s, sep string) (arr []int64) { | 14 | func ParseInt64Arr(s, sep string) (arr []int64) { |
15 | s = strings.TrimSpace(s) | 15 | s = strings.TrimSpace(s) |
16 | if s != "" { | 16 | if s != "" { |
17 | parts := strings.Split(s, sep) | 17 | parts := strings.Split(s, sep) |
18 | arr = make([]int64, len(parts)) | 18 | arr = make([]int64, len(parts)) |
19 | for i, p := range parts { | 19 | for i, p := range parts { |
20 | num := StringToInt64(p) | 20 | num := StringToInt64(p) |
21 | arr[i] = num | 21 | arr[i] = num |
22 | } | 22 | } |
23 | } | 23 | } |
24 | 24 | ||
25 | return arr | 25 | return arr |
26 | } | 26 | } |
27 | 27 | ||
28 | // Int64SliceToString ... | 28 | // Int64SliceToString ... |
29 | func Int64SliceToString(arr []int64) (s string) { | 29 | func Int64SliceToString(arr []int64) (s string) { |
30 | for i, num := range arr { | 30 | for i, num := range arr { |
31 | if i == 0 { | 31 | if i == 0 { |
32 | s += fmt.Sprintf("%d", num) | 32 | s += fmt.Sprintf("%d", num) |
33 | } else { | 33 | } else { |
34 | s += fmt.Sprintf(",%d", num) | 34 | s += fmt.Sprintf(",%d", num) |
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | return s | 38 | return s |
39 | } | 39 | } |
40 | 40 | ||
41 | // CombineStrings ... | 41 | // CombineStrings ... |
42 | func CombineStrings(s1, s2, s3 string) string { | 42 | func CombineStrings(s1, s2, s3 string) string { |
43 | s1 = strings.TrimSpace(s1) | 43 | s1 = strings.TrimSpace(s1) |
44 | s2 = strings.TrimSpace(s2) | 44 | s2 = strings.TrimSpace(s2) |
45 | 45 | ||
46 | if s1 != "" && s2 != "" { | 46 | if s1 != "" && s2 != "" { |
47 | s1 += s3 + s2 | 47 | s1 += s3 + s2 |
48 | } else { | 48 | } else { |
49 | s1 += s2 | 49 | s1 += s2 |
50 | } | 50 | } |
51 | 51 | ||
52 | return s1 | 52 | return s1 |
53 | } | 53 | } |
54 | 54 | ||
55 | // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string. | 55 | // ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string. |
56 | func ReplaceAny(s, patern, r string) (n string) { | 56 | func ReplaceAny(s, patern, r string) (n string) { |
57 | n = s | 57 | n = s |
58 | for _, c := range patern { | 58 | for _, c := range patern { |
59 | n = strings.Replace(n, string(c), r, -1) | 59 | n = strings.Replace(n, string(c), r, -1) |
60 | } | 60 | } |
61 | return n | 61 | return n |
62 | } | 62 | } |
63 | 63 | ||
64 | func StringToBool(s string) bool { | 64 | func StringToBool(s string) bool { |
65 | s = strings.ToLower(s) | 65 | s = strings.ToLower(s) |
66 | if s == "true" { | 66 | if s == "true" { |
67 | return true | 67 | return true |
68 | } | 68 | } |
69 | return false | 69 | return false |
70 | } | 70 | } |
71 | 71 | ||
72 | func BoolToString(b bool) string { | 72 | func BoolToString(b bool) string { |
73 | return fmt.Sprintf("%b", b) | 73 | return fmt.Sprintf("%b", b) |
74 | } | 74 | } |
75 | 75 | ||
76 | func StringSliceContains(slice []string, s string) bool { | 76 | func StringSliceContains(slice []string, s string) bool { |
77 | for i := range slice { | 77 | for i := range slice { |
78 | if slice[i] == s { | 78 | if slice[i] == s { |
79 | return true | 79 | return true |
80 | } | 80 | } |
81 | } | 81 | } |
82 | return false | 82 | return false |
83 | } | 83 | } |
84 | |||
85 | func StringAt(s string, index int) string { | ||
86 | if len(s)-1 < index || index < 0 { | ||
87 | return "" | ||
88 | } | ||
89 | |||
90 | return string(s[index]) | ||
91 | } | ||
92 | |||
93 | func SplitStringAfterN(s string, n, count int) (parts []string) { | ||
94 | rem := s | ||
95 | i := 0 | ||
96 | for len(rem) > n && (i < count || count < 0) { | ||
97 | parts = append(parts, rem[0:n]) | ||
98 | rem = rem[n:] | ||
99 | i++ | ||
100 | } | ||
101 | parts = append(parts, rem) | ||
102 | return parts | ||
103 | } | ||
84 | 104 |