-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring.go
45 lines (40 loc) · 1.06 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
package aoc
import (
"strconv"
"strings"
)
// ReverseString reverses a string.
func ReverseString(str string) string {
var result string
for _, i := range str {
result = string(i) + result
}
return result
}
// Atoi converts a string to an integer without caring about errors.
func Atoi(str string) int {
i, _ := strconv.Atoi(str)
return i
}
// ContainsCharacters returns true if the string contains the given characters anywhere in any order.
func ContainsCharacters(str string, characters string) bool {
for _, i := range characters {
if !strings.Contains(str, string(i)) {
return false
}
}
return true
}
// ContainsExactly returns true if the string contains the given characters and has the same length.
func ContainsExactly(str string, characters string) bool {
if len(str) != len(characters) {
return false
}
for _, i := range characters {
// Yes that is a bit wacky if the string contains the same character multiple times, though worked for the challenge in 2021.
if !strings.Contains(str, string(i)) {
return false
}
}
return true
}