-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Change-Id: Ie2d0cbd62d87c8e7d48bb87fc7db0556640dea0a Dispatch-Trailer: {"type":"trybot","CL":1174748,"patchset":12,"ref":"refs/changes/48/1174748/12","targetBranch":"alpha"}
- Loading branch information
1 parent
d92e0c1
commit 00f5ae2
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...nt/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
18 changes: 18 additions & 0 deletions
18
content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/gen_cache.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
content/docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/page.cue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package site | ||
|
||
content: docs: howto: "use-the-built-in-function-strings-replace-to-modify-strings": {} |
56 changes: 56 additions & 0 deletions
56
...docs/howto/use-the-built-in-function-strings-replace-to-modify-strings/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |