From 00f5ae2dec4a1275596fae8cd964ef49e3f2b961 Mon Sep 17 00:00:00 2001 From: Jonathan Matthews Date: Thu, 18 Jan 2024 15:28:44 +0000 Subject: [PATCH] docs/howto: use strings.Replace to modify strings This adds a Commented CUE guide demonstrating how to use strings.Replace. Preview-Path: /docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/ Signed-off-by: Jonathan Matthews Change-Id: Ie2d0cbd62d87c8e7d48bb87fc7db0556640dea0a Dispatch-Trailer: {"type":"trybot","CL":1174748,"patchset":12,"ref":"refs/changes/48/1174748/12","targetBranch":"alpha"} --- .../en.md | 57 +++++++++++++++++++ .../gen_cache.cue | 18 ++++++ .../page.cue | 3 + .../index.md | 56 ++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/en.md create mode 100644 content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/gen_cache.cue create mode 100644 content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/page.cue create mode 100644 hugo/content/en/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/index.md diff --git a/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/en.md b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/en.md new file mode 100644 index 000000000..cf387bde2 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/en.md @@ -0,0 +1,57 @@ +--- +title: Using the built-in function "strings.Replace" to modify strings +tags: +- commented cue +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`strings.Replace`](https://pkg.go.dev/cuelang.org/go/pkg/strings#Replace) +to modify strings, *without* using regular expressions. + +{{{with code "en" "cc"}}} +#location top bottom + +exec cue export +cmp stdout out +-- file.cue -- +package example + +import "strings" + +// strings.Replace takes 4 parameters: +// - the string to modify +// - the old string (to search for and replace) +// - the new string (to take the old string's place) +// - the maximum number of replacements to make, counting from +// the start of the string +replace: strings.Replace("This string repeats the word 'JSON': JSON, JSON, JSON.", "JSON", "YAML", 3) + +// A negative value for the 4th parameter (the number of replacements to make) +// means "make unlimited replacements". +replaceAll: strings.Replace("one one one one one one one", "one", "two", -1) + +// Replaced and replacement strings are fixed values, not regular expressions. +fixed: strings.Replace("Parameters are fixed strings values.", ".*", "REPLACED", -1) + +// Any parameter may be provided via reference. +reference: strings.Replace(_modify, _old, _new, _count) +_modify: "Some string value" +_old: "string" +_new: "STRING" +_count: -1 +-- out -- +{ + "replace": "This string repeats the word 'YAML': YAML, YAML, JSON.", + "replaceAll": "two two two two two two two", + "fixed": "Parameters are fixed strings values.", + "reference": "Some STRING value" +} +{{{end}}} + +## Related content + +- The [`strings`](https://pkg.go.dev/cuelang.org/go/pkg/strings) built-in package diff --git a/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/gen_cache.cue b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/gen_cache.cue new file mode 100644 index 000000000..b34f4a1b8 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-the-built-in-function-strings-replace-to-modify-strings": { + page: { + cache: { + code: { + cc: "81hv8JSnsjEDt+xdoRWmkTLBmT4HNOD0Wj1t4bUyw60=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/page.cue b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/page.cue new file mode 100644 index 000000000..2bd8c4629 --- /dev/null +++ b/content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-the-built-in-function-strings-replace-to-modify-strings": {} diff --git a/hugo/content/en/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/index.md b/hugo/content/en/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/index.md new file mode 100644 index 000000000..46529739c --- /dev/null +++ b/hugo/content/en/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/index.md @@ -0,0 +1,56 @@ +--- +title: Using the built-in function "strings.Replace" to modify strings +tags: +- commented cue +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`strings.Replace`](https://pkg.go.dev/cuelang.org/go/pkg/strings#Replace) +to modify strings, *without* using regular expressions. + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top" >}} +package example + +import "strings" + +// strings.Replace takes 4 parameters: +// - the string to modify +// - the old string (to search for and replace) +// - the new string (to take the old string's place) +// - the maximum number of replacements to make, counting from +// the start of the string +replace: strings.Replace("This string repeats the word 'JSON': JSON, JSON, JSON.", "JSON", "YAML", 3) + +// A negative value for the 4th parameter (the number of replacements to make) +// means "make unlimited replacements". +replaceAll: strings.Replace("one one one one one one one", "one", "two", -1) + +// Replaced and replacement strings are fixed values, not regular expressions. +fixed: strings.Replace("Parameters are fixed strings values.", ".*", "REPLACED", -1) + +// Any parameter may be provided via reference. +reference: strings.Replace(_modify, _old, _new, _count) +_modify: "Some string value" +_old: "string" +_new: "STRING" +_count: -1 +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} +$ cue export +{ + "replace": "This string repeats the word 'YAML': YAML, YAML, JSON.", + "replaceAll": "two two two two two two two", + "fixed": "Parameters are fixed strings values.", + "reference": "Some STRING value" +} +{{< /code-tab >}} +{{< /code-tabs >}} + +## Related content + +- The [`strings`](https://pkg.go.dev/cuelang.org/go/pkg/strings) built-in package