From 21664a8ffb37ec83bfb8f717ef723c503eed6e5a Mon Sep 17 00:00:00 2001 From: Karl Schriek Date: Mon, 10 Jan 2022 03:24:34 +0200 Subject: Add strings.Split example to strings.md (#1279) Add additional examples to clarifiy typical use cases for strings.Split. Resolves https://github.com/hairyhenderson/gomplate/issues/1278 Co-authored-by: Dave Henderson --- docs/content/functions/strings.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'docs') diff --git a/docs/content/functions/strings.md b/docs/content/functions/strings.md index a3830771..65b4193c 100644 --- a/docs/content/functions/strings.md +++ b/docs/content/functions/strings.md @@ -229,6 +229,13 @@ input | strings.Split separator ### Examples +Use on its own to produce an array: +```console +$ gomplate -i '{{ "Bart,Lisa,Maggie" | strings.Split "," }}' +[Bart Lisa Maggie] +``` + +Use in combination with `range` to iterate over all items: ```console $ gomplate -i '{{range ("Bart,Lisa,Maggie" | strings.Split ",") }}Hello, {{.}} {{end}}' @@ -237,6 +244,13 @@ Hello, Lisa Hello, Maggie ``` +Use in combination with `index` function to pick a specific value from the resulting array +```console +$ gomplate -i '{{index ("Bart,Lisa,Maggie" | strings.Split ",") 0 }}' +Bart +``` + + ## `strings.SplitN` Creates a slice by splitting a string on a given delimiter. The count determines -- cgit v1.2.3