-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
72 lines (59 loc) · 1.67 KB
/
string.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
package goutility
import (
"strings"
"math/rand"
)
// StringAppendWithJoin append two strings with a join string accounting for empty strings
func StringAppendWithJoin(left string, join string, right string) (result string) {
if len(left) > 0 {
if len(right) > 0 {
result = left + join + right
} else {
result = left
}
} else {
result = right
}
return
}
// PadStringToLength pad string with string until it is equal to or greater than length param and return the result
func PadStringToLength(original string, padWith string, length int) string {
result := original
for {
if len(result) >= length {
break
}
result = result + padWith
}
return result
}
// PathStrippedOfQuery return the path minus any query
func PathStrippedOfQuery(path string) string {
splits := strings.Split(path, "{")
if len(splits) > 0 {
return splits[0]
}
return path
}
// FirstCharacter return the first character in a string
func FirstCharacter(in string) string {
return string([]rune(in)[0])
}
// StringOfStringRepeated return a string composed of component repeated count times
func StringOfStringRepeated(component string, count int) string {
components := []string{}
for times := 1; times <= count; times++ {
components = append(components, component)
}
return strings.Join(components[:], "")
}
// http://stackoverflow.com/a/31832326
var randomStringLetterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// RandomString create a random string of specified length
func RandomString(length int) string {
b := make([]rune, length)
for i := range b {
b[i] = randomStringLetterRunes[rand.Intn(len(randomStringLetterRunes))]
}
return string(b)
}