-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.go
41 lines (35 loc) · 1009 Bytes
/
str.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
package utils
import "strings"
// StrBytes returns []byte form of its input string
func StrBytes(p string) []byte {
return []byte(p)
}
// RemoveDuplicateStrs returns input array without duplicates
func RemoveDuplicateStrs(strSlice []string) (list []string) {
keys := make(map[string]bool)
// If the key(values of the slice) is not equal
// to the already present value in new slice (list)
// then we append it. else we jump on another element.
for _, entry := range strSlice {
entry = strings.TrimSpace(entry)
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return
}
// RemoveStr removes an item from string slice.
func RemoveStr(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}
// PopStrAtIndex pops an string item from string slice by index.
func PopStrAtIndex(s []string, i uint32) ([]string, string) {
popElem := s[i]
return append(s[:i], s[i+1:]...), popElem
}