| 1 |
# String Slice Functions |
| 2 |
|
| 3 |
These function operate on or generate slices of strings. In Go, a slice is a |
| 4 |
growable array. In Sprig, it's a special case of a `list`. |
| 5 |
|
| 6 |
## join |
| 7 |
|
| 8 |
Join a list of strings into a single string, with the given separator. |
| 9 |
|
| 10 |
``` |
| 11 |
list "hello" "world" | join "_" |
| 12 |
``` |
| 13 |
|
| 14 |
The above will produce `hello_world` |
| 15 |
|
| 16 |
`join` will try to convert non-strings to a string value: |
| 17 |
|
| 18 |
``` |
| 19 |
list 1 2 3 | join "+" |
| 20 |
``` |
| 21 |
|
| 22 |
The above will produce `1+2+3` |
| 23 |
|
| 24 |
## splitList and split |
| 25 |
|
| 26 |
Split a string into a list of strings: |
| 27 |
|
| 28 |
``` |
| 29 |
splitList "$" "foo$bar$baz" |
| 30 |
``` |
| 31 |
|
| 32 |
The above will return `[foo bar baz]` |
| 33 |
|
| 34 |
The older `split` function splits a string into a `dict`. It is designed to make |
| 35 |
it easy to use template dot notation for accessing members: |
| 36 |
|
| 37 |
``` |
| 38 |
$a := split "$" "foo$bar$baz" |
| 39 |
``` |
| 40 |
|
| 41 |
The above produces a map with index keys. `{_0: foo, _1: bar, _2: baz}` |
| 42 |
|
| 43 |
``` |
| 44 |
$a._0 |
| 45 |
``` |
| 46 |
|
| 47 |
The above produces `foo` |
| 48 |
|
| 49 |
## splitn |
| 50 |
|
| 51 |
`splitn` function splits a string into a `dict`. It is designed to make |
| 52 |
it easy to use template dot notation for accessing members: |
| 53 |
|
| 54 |
``` |
| 55 |
$a := splitn "$" 2 "foo$bar$baz" |
| 56 |
``` |
| 57 |
|
| 58 |
The above produces a map with index keys. `{_0: foo, _1: bar$baz}` |
| 59 |
|
| 60 |
``` |
| 61 |
$a._0 |
| 62 |
``` |
| 63 |
|
| 64 |
The above produces `foo` |
| 65 |
|
| 66 |
## sortAlpha |
| 67 |
|
| 68 |
The `sortAlpha` function sorts a list of strings into alphabetical (lexicographical) |
| 69 |
order. |
| 70 |
|
| 71 |
It does _not_ sort in place, but returns a sorted copy of the list, in keeping |
| 72 |
with the immutability of lists. |