Skip to content

Commit

Permalink
docs/howto: use strings.Replace to modify strings
Browse files Browse the repository at this point in the history
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":3,"ref":"refs/changes/48/1174748/3","targetBranch":"alpha"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jan 22, 2024
1 parent 51a6404 commit 469e5bc
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
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
inline: 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 --
{
"inline": "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
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: "R9uxEac4QeuF19npHgmgcsq/PzKcG+F9Uux1eI+flqE="
}
}
}
}
}
}
}
}
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": {}
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
inline: 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
{
"inline": "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

0 comments on commit 469e5bc

Please sign in to comment.