summaryrefslogtreecommitdiff
path: root/go/series/series.go
blob: 55f7eb2673aef8bcf6d41c1523706a15ead2d2e6 (plain)
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
package slice

// All returns a list of all substrings of s with length n.
func All(n int, s string) []string {
	var ret []string
	for i, j := 0, n; j <= len(s); i, j = i+1, j+1 {
		ret = append(ret, s[i:j])
	}
	return ret
}

// UnsafeFirst returns the first substring of s with length n.
func UnsafeFirst(n int, s string) string {
	if len(s) >= n {
		return s[:n]
	}
	return ""
}

func First(n int, s string) (string, bool) {
	if len(s) >= n {
		return s[:n], true
	}
	return "", false
}