diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-10-20 09:43:56 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-10-20 09:43:56 +0200 |
commit | e2dd4cd05fa96a08d49b3b198edf0ccf9a94970e (patch) | |
tree | 712334f7e7a657155706f556040575bea9b7757f /docs/content/en/functions/strings | |
parent | fd381718101a35a5f5f92d5a05b3a0c36ef50db0 (diff) | |
parent | e509cac533600cf4fa8382c9cdab78ddd82db688 (diff) | |
download | hugo-e2dd4cd05fa96a08d49b3b198edf0ccf9a94970e.tar.gz hugo-e2dd4cd05fa96a08d49b3b198edf0ccf9a94970e.zip |
Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'
Diffstat (limited to 'docs/content/en/functions/strings')
28 files changed, 927 insertions, 0 deletions
diff --git a/docs/content/en/functions/strings/Chomp.md b/docs/content/en/functions/strings/Chomp.md new file mode 100644 index 000000000..22e2b546b --- /dev/null +++ b/docs/content/en/functions/strings/Chomp.md @@ -0,0 +1,31 @@ +--- +title: chomp +linkTitle: chomp +description: Removes any trailing newline characters. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [chomp] + returnType: any + signatures: [strings.Chomp STRING] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/chomp] +--- + +If the argument is of type template.HTML, returns template.HTML, else returns a string. + + +Useful in a pipeline to remove newlines added by other processing (e.g., [`markdownify`](/functions/transform/markdownify)). + +```go-html-template +{{ chomp "<p>Blockhead</p>\n" }} → "<p>Blockhead</p>" +``` diff --git a/docs/content/en/functions/strings/Contains.md b/docs/content/en/functions/strings/Contains.md new file mode 100644 index 000000000..66a90aeea --- /dev/null +++ b/docs/content/en/functions/strings/Contains.md @@ -0,0 +1,29 @@ +--- +title: strings.Contains +description: Reports whether the string contains a substring. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: bool + signatures: [strings.Contains STRING SUBSTRING] +relatedFunctions: + - strings.Contains + - strings.ContainsAny + - strings.ContainsNonSpace + - strings.HasPrefix + - strings.HasSuffix +aliases: [/functions/strings.contains] +--- + +```go-html-template +{{ strings.Contains "Hugo" "go" }} → true +``` +The check is case sensitive: + +```go-html-template +{{ strings.Contains "Hugo" "Go" }} → false +``` diff --git a/docs/content/en/functions/strings/ContainsAny.md b/docs/content/en/functions/strings/ContainsAny.md new file mode 100644 index 000000000..4f324358a --- /dev/null +++ b/docs/content/en/functions/strings/ContainsAny.md @@ -0,0 +1,30 @@ +--- +title: strings.ContainsAny +description: Reports whether a string contains any character from a given string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: bool + signatures: [strings.ContainsAny STRING CHARACTERS] +relatedFunctions: + - strings.Contains + - strings.ContainsAny + - strings.ContainsNonSpace + - strings.HasPrefix + - strings.HasSuffix +aliases: [/functions/strings.containsany] +--- + +```go-html-template +{{ strings.ContainsAny "Hugo" "gm" }} → true +``` + +The check is case sensitive: + +```go-html-template +{{ strings.ContainsAny "Hugo" "Gm" }} → false +``` diff --git a/docs/content/en/functions/strings/ContainsNonSpace.md b/docs/content/en/functions/strings/ContainsNonSpace.md new file mode 100644 index 000000000..d2e6114b3 --- /dev/null +++ b/docs/content/en/functions/strings/ContainsNonSpace.md @@ -0,0 +1,36 @@ +--- +title: strings.ContainsNonSpace +description: Reports whether a string contains any non-space characters as defined by Unicode’s White Space property. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: bool + signatures: [strings.ContainsNonSpace STRING] +relatedFunctions: + - strings.Contains + - strings.ContainsAny + - strings.ContainsNonSpace + - strings.HasPrefix + - strings.HasSuffix +aliases: [/functions/strings.containsnonspace] +--- + +```go-html-template +{{ strings.ContainsNonSpace "\n" }} → false +{{ strings.ContainsNonSpace " " }} → false +{{ strings.ContainsNonSpace "\n abc" }} → true +``` + +Common white space characters include: + +```text +'\t', '\n', '\v', '\f', '\r', ' ' +``` + +See the [Unicode Character Database] for a complete list. + +[Unicode Character Database]: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt diff --git a/docs/content/en/functions/strings/Count.md b/docs/content/en/functions/strings/Count.md new file mode 100644 index 000000000..25ea58967 --- /dev/null +++ b/docs/content/en/functions/strings/Count.md @@ -0,0 +1,29 @@ +--- +title: strings.Count +description: Returns the number of non-overlapping instances of a substring within a string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: int + signatures: [strings.Count SUBSTR STRING] +relatedFunctions: + - len + - strings.Count + - strings.CountRunes + - strings.CountWords + - strings.RuneCount +aliases: [/functions/strings.count] +--- + +If `SUBSTR` is an empty string, this function returns 1 plus the number of Unicode code points in `STRING`. + +```go-html-template +{{ "aaabaab" | strings.Count "a" }} → 5 +{{ "aaabaab" | strings.Count "aa" }} → 2 +{{ "aaabaab" | strings.Count "aaa" }} → 1 +{{ "aaabaab" | strings.Count "" }} → 8 +``` diff --git a/docs/content/en/functions/strings/CountRunes.md b/docs/content/en/functions/strings/CountRunes.md new file mode 100644 index 000000000..4a17d04ab --- /dev/null +++ b/docs/content/en/functions/strings/CountRunes.md @@ -0,0 +1,29 @@ +--- +title: strings.CountRunes +linkTitle: countrunes +description: Returns the number of runes in a string excluding whitespace. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [countrunes] + returnType: int + signatures: [strings.CountRunes INPUT] +relatedFunctions: + - len + - strings.Count + - strings.CountRunes + - strings.CountWords + - strings.RuneCount +aliases: [/functions/countrunes] +--- + +In contrast with the [`strings.RuneCount`] function, which counts every rune in a string, `strings.CountRunes` excludes whitespace. + +```go-html-template +{{ "Hello, 世界" | strings.CountRunes }} → 8 +``` + +[`strings.RuneCount`]: /functions/strings/runecount diff --git a/docs/content/en/functions/strings/CountWords.md b/docs/content/en/functions/strings/CountWords.md new file mode 100644 index 000000000..e6915e6cd --- /dev/null +++ b/docs/content/en/functions/strings/CountWords.md @@ -0,0 +1,31 @@ +--- +title: strings.CountWords +linkTitle: countwords +description: Counts the number of words in a string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [countwords] + returnType: int + signatures: [strings.CountWords INPUT] +relatedFunctions: + - len + - strings.Count + - strings.CountRunes + - strings.CountWords + - strings.RuneCount +aliases: [/functions/countwords] +--- + +The template function works similar to the [.WordCount page variable][pagevars]. + +```go-html-template +{{ "Hugo is a static site generator." | countwords }} +<!-- outputs a content length of 6 words. --> +``` + + +[pagevars]: /variables/page/ diff --git a/docs/content/en/functions/strings/FindRESubmatch.md b/docs/content/en/functions/strings/FindRESubmatch.md new file mode 100644 index 000000000..5a0410fdb --- /dev/null +++ b/docs/content/en/functions/strings/FindRESubmatch.md @@ -0,0 +1,95 @@ +--- +title: strings.FindRESubmatch +linkTitle: findRESubmatch +description: Returns a slice of all successive matches of the regular expression. Each element is a slice of strings holding the text of the leftmost match of the regular expression and the matches, if any, of its subexpressions. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [findRESubmatch] + returnType: '[]string' + signatures: ['strings.FindRESubmatch PATTERN INPUT [LIMIT]'] +relatedFunctions: + - strings.FindRE + - strings.FindRESubmatch + - strings.Replace + - strings.ReplaceRE +aliases: [/functions/findresubmatch] +--- + +By default, `findRESubmatch` finds all matches. You can limit the number of matches with an optional LIMIT argument. A return value of nil indicates no match. + +{{% readfile file="/functions/_common/regular-expressions.md" %}} + +## Demonstrative examples + +```go-html-template +{{ findRESubmatch `a(x*)b` "-ab-" }} → [["ab" ""]] +{{ findRESubmatch `a(x*)b` "-axxb-" }} → [["axxb" "xx"]] +{{ findRESubmatch `a(x*)b` "-ab-axb-" }} → [["ab" ""] ["axb" "x"]] +{{ findRESubmatch `a(x*)b` "-axxb-ab-" }} → [["axxb" "xx"] ["ab" ""]] +{{ findRESubmatch `a(x*)b` "-axxb-ab-" 1 }} → [["axxb" "xx"]] +``` + +## Practical example + +This markdown: + +```text +- [Example](https://example.org) +- [Hugo](https://gohugo.io) +``` + +Produces this HTML: + +```html +<ul> + <li><a href="https://example.org">Example</a></li> + <li><a href="https://gohugo.io">Hugo</a></li> +</ul> +``` + +To match the anchor elements, capturing the link destination and text: + +```go-html-template +{{ $regex := `<a\s*href="(.+?)">(.+?)</a>` }} +{{ $matches := findRESubmatch $regex .Content }} +``` + +Viewed as JSON, the data structure of `$matches` in the code above is: + +```json +[ + [ + "<a href=\"https://example.org\"></a>Example</a>", + "https://example.org", + "Example" + ], + [ + "<a href=\"https://gohugo.io\">Hugo</a>", + "https://gohugo.io", + "Hugo" + ] +] +``` + +To render the `href` attributes: + +```go-html-template +{{ range $matches }} + {{ index . 1 }} +{{ end }} +``` + +Result: + +```text +https://example.org +https://gohugo.io +``` + +{{% note %}} +You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin. +{{% /note %}} diff --git a/docs/content/en/functions/strings/FindRe.md b/docs/content/en/functions/strings/FindRe.md new file mode 100644 index 000000000..4a7811f3d --- /dev/null +++ b/docs/content/en/functions/strings/FindRe.md @@ -0,0 +1,41 @@ +--- +title: strings.FindRE +linkTitle: findRE +description: Returns a slice of strings that match the regular expression. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [findRE] + returnType: string + signatures: ['strings.FindRE PATTERN INPUT [LIMIT]'] +relatedFunctions: + - strings.FindRE + - strings.FindRESubmatch + - strings.Replace + - strings.ReplaceRE +aliases: [/functions/findre] +--- +By default, `findRE` finds all matches. You can limit the number of matches with an optional LIMIT argument. + +{{% readfile file="/functions/_common/regular-expressions.md" %}} + +This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`: + +```go-html-template +{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }} +``` + +The `s` flag causes `.` to match `\n` as well, allowing us to find an `h2` element that contains newlines. + +To limit the number of matches to one: + +```go-html-template +{{ findRE `(?s)<h2.*?>.*?</h2>` .Content 1 }} +``` + +{{% note %}} +You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin. +{{% /note %}} diff --git a/docs/content/en/functions/strings/FirstUpper.md b/docs/content/en/functions/strings/FirstUpper.md new file mode 100644 index 000000000..320f01eda --- /dev/null +++ b/docs/content/en/functions/strings/FirstUpper.md @@ -0,0 +1,23 @@ +--- +title: strings.FirstUpper +description: Capitalizes the first character of a given string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.FirstUpper STRING] +relatedFunctions: + - strings.FirstUpper + - strings.Title + - strings.ToLower + - strings.ToUpper +aliases: [/functions/strings.firstupper] +--- + +```go-html-template +{{ strings.FirstUpper "foo" }} → "Foo" +``` diff --git a/docs/content/en/functions/strings/HasPrefix.md b/docs/content/en/functions/strings/HasPrefix.md new file mode 100644 index 000000000..88a79a935 --- /dev/null +++ b/docs/content/en/functions/strings/HasPrefix.md @@ -0,0 +1,24 @@ +--- +title: strings.HasPrefix +description: Reports whether a string begins with prefix. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [hasPrefix] + returnType: bool + signatures: [strings.HasPrefix STRING PREFIX] +relatedFunctions: + - strings.Contains + - strings.ContainsAny + - strings.ContainsNonSpace + - strings.HasPrefix + - strings.HasSuffix +aliases: [/functions/hasprefix,/functions/strings.hasprefix] +--- + +```go-html-template +{{ hasPrefix "Hugo" "Hu" }} → true +``` diff --git a/docs/content/en/functions/strings/HasSuffix.md b/docs/content/en/functions/strings/HasSuffix.md new file mode 100644 index 000000000..d11f3e8cf --- /dev/null +++ b/docs/content/en/functions/strings/HasSuffix.md @@ -0,0 +1,24 @@ +--- +title: strings.HasSuffix +description: Reports whether a string ends with suffix. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [hasSuffix] + returnType: bool + signatures: [strings.HasSuffix STRING SUFFIX] +relatedFunctions: + - strings.Contains + - strings.ContainsAny + - strings.ContainsNonSpace + - strings.HasPrefix + - strings.HasSuffix +aliases: [/functions/hassuffix,/functions/strings/hassuffix] +--- + +```go-html-template +{{ hasSuffix "Hugo" "go" }} → true +``` diff --git a/docs/content/en/functions/strings/Repeat.md b/docs/content/en/functions/strings/Repeat.md new file mode 100644 index 000000000..718f24984 --- /dev/null +++ b/docs/content/en/functions/strings/Repeat.md @@ -0,0 +1,20 @@ +--- +title: strings.Repeat +description: Returns a new string consisting of zero or more copies of another string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.Repeat COUNT INPUT] +relatedFunctions: [] +aliases: [/functions/strings.repeat] +--- + +```go-html-template +{{ strings.Repeat 3 "yo" }} → "yoyoyo" +{{ "yo" | strings.Repeat 3 }} → "yoyoyo" +``` diff --git a/docs/content/en/functions/strings/Replace.md b/docs/content/en/functions/strings/Replace.md new file mode 100644 index 000000000..8d5e54859 --- /dev/null +++ b/docs/content/en/functions/strings/Replace.md @@ -0,0 +1,30 @@ +--- +title: strings.Replace +linkTitle: replace +description: Replaces all occurrences of the search string with the replacement string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [replace] + returnType: string + signatures: ['strings.Replace INPUT OLD NEW [LIMIT]'] +relatedFunctions: + - strings.FindRE + - strings.FindRESubmatch + - strings.Replace + - strings.ReplaceRE +aliases: [/functions/replace] +--- + +Replace returns a copy of `INPUT` with all occurrences of `OLD` replaced with `NEW`. +The number of replacements can be limited with an optional `LIMIT` argument. + +``` +{{ replace "Batman and Robin" "Robin" "Catwoman" }} +→ "Batman and Catwoman" + +{{ replace "aabbaabb" "a" "z" 2 }} → "zzbbaabb" +``` diff --git a/docs/content/en/functions/strings/ReplaceRE.md b/docs/content/en/functions/strings/ReplaceRE.md new file mode 100644 index 000000000..247595877 --- /dev/null +++ b/docs/content/en/functions/strings/ReplaceRE.md @@ -0,0 +1,51 @@ +--- +title: strings.ReplaceRE +linkTitle: replaceRE +description: Returns a string, replacing all occurrences of a regular expression with a replacement pattern. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [replaceRE] + returnType: string + signatures: ['strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]'] +relatedFunctions: + - strings.FindRE + - strings.FindRESubmatch + - strings.Replace + - strings.ReplaceRE +aliases: [/functions/replacere] +--- +By default, `replaceRE` replaces all matches. You can limit the number of matches with an optional LIMIT argument. + +{{% readfile file="/functions/_common/regular-expressions.md" %}} + +This example replaces two or more consecutive hyphens with a single hyphen: + +```go-html-template +{{ $s := "a-b--c---d" }} +{{ replaceRE `(-{2,})` "-" $s }} → a-b-c-d +``` + +To limit the number of replacements to one: + +```go-html-template +{{ $s := "a-b--c---d" }} +{{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---d +``` + +You can use `$1`, `$2`, etc. within the replacement string to insert the groups captured within the regular expression: + +```go-html-template +{{ $s := "http://gohugo.io/docs" }} +{{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.io +``` + +{{% note %}} +You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin. +{{% /note %}} + +[RE2]: https://github.com/google/re2/wiki/Syntax +[string literal]: https://go.dev/ref/spec#String_literals diff --git a/docs/content/en/functions/strings/RuneCount.md b/docs/content/en/functions/strings/RuneCount.md new file mode 100644 index 000000000..a4d5a8dbe --- /dev/null +++ b/docs/content/en/functions/strings/RuneCount.md @@ -0,0 +1,28 @@ +--- +title: strings.RuneCount +description: Returns the number of runes in a string. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: int + signatures: [strings.RuneCount INPUT] +relatedFunctions: + - len + - strings.Count + - strings.CountRunes + - strings.CountWords + - strings.RuneCount +aliases: [/functions/strings.runecount] +--- + +In contrast with the [`strings.CountRunes`] function, which excludes whitespace, `strings.RuneCount` counts every rune in a string. + +```go-html-template +{{ "Hello, 世界" | strings.RuneCount }} → 9 +``` + +[`strings.CountRunes`]: /functions/strings/countrunes diff --git a/docs/content/en/functions/strings/SliceString.md b/docs/content/en/functions/strings/SliceString.md new file mode 100644 index 000000000..8d26d76e4 --- /dev/null +++ b/docs/content/en/functions/strings/SliceString.md @@ -0,0 +1,24 @@ +--- +title: strings.SliceString +linkTitle: slicestr +description: Creates a slice of a half-open range, including start and end indices. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [slicestr] + returnType: string + signatures: ['strings.SliceString STRING START [END]'] +relatedFunctions: [] +aliases: [/functions/slicestr] +--- + +For example, 1 and 4 creates a slice including elements 1 through 3. +The `end` index can be omitted; it defaults to the string's length. + +```go-html-template +{{ slicestr "BatMan" 3 }}` → "Man" +{{ slicestr "BatMan" 0 3 }}` → "Bat" +``` diff --git a/docs/content/en/functions/strings/Split.md b/docs/content/en/functions/strings/Split.md new file mode 100644 index 000000000..7d15704b2 --- /dev/null +++ b/docs/content/en/functions/strings/Split.md @@ -0,0 +1,30 @@ +--- +title: strings.Split +linkTitle: split +description: Returns a slice of strings by splitting STRING by DELIM. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [split] + returnType: string + signatures: [strings.Split STRING DELIM] +relatedFunctions: + - collections.Delimit + - strings.Split +aliases: [/functions/split] +--- + +Examples: + +```go-html-template +{{ split "tag1,tag2,tag3" "," }} → ["tag1", "tag2", "tag3"] +{{ split "abc" "" }} → ["a", "b", "c"] +``` + + +{{% note %}} +`split` essentially does the opposite of [delimit](/functions/collections/delimit). While `split` creates a slice from a string, `delimit` creates a string from a slice. +{{% /note %}} diff --git a/docs/content/en/functions/strings/Substr.md b/docs/content/en/functions/strings/Substr.md new file mode 100644 index 000000000..9dafa0737 --- /dev/null +++ b/docs/content/en/functions/strings/Substr.md @@ -0,0 +1,42 @@ +--- +title: strings.Substr +linkTitle: substr +description: Extracts parts of a string from a specified character's position and returns the specified number of characters. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [substr] + returnType: string + signatures: ['strings.Substr STRING START [LENGTH]'] +relatedFunctions: [] +aliases: [/functions/substr] +--- + +It normally takes two argument: `start` and `length`. It can also take one argument: `start`, i.e. `length` is omitted, in which case the substring starting from start until the end of the string will be returned. + +To extract characters from the end of the string, use a negative start number. + +If `length` is given and is negative, that number of characters will be omitted from the end of string. + +```go-html-template +{{ substr "abcdef" 0 }} → "abcdef" +{{ substr "abcdef" 1 }} → "bcdef" + +{{ substr "abcdef" 0 1 }} → "a" +{{ substr "abcdef" 1 1 }} → "b" + +{{ substr "abcdef" 0 -1 }} → "abcde" +{{ substr "abcdef" 1 -1 }} → "bcde" + +{{ substr "abcdef" -1 }} → "f" +{{ substr "abcdef" -2 }} → "ef" + +{{ substr "abcdef" -1 1 }} → "f" +{{ substr "abcdef" -2 1 }} → "e" + +{{ substr "abcdef" -3 -1 }} → "de" +{{ substr "abcdef" -3 -2 }} → "d" +``` diff --git a/docs/content/en/functions/strings/Title.md b/docs/content/en/functions/strings/Title.md new file mode 100644 index 000000000..1e20d1f59 --- /dev/null +++ b/docs/content/en/functions/strings/Title.md @@ -0,0 +1,30 @@ +--- +title: strings.Title +linkTitle: title +description: Converts the provided string to title case. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [title] + returnType: string + signatures: [strings.Title STRING] +relatedFunctions: + - strings.FirstUpper + - strings.Title + - strings.ToLower + - strings.ToUpper +aliases: [/functions/title] +--- + +```go-html-template +{{ title "table of contents (TOC)" }} → "Table of Contents (TOC)" +``` + +By default, Hugo adheres to the capitalization rules in the [Associated Press (AP) Stylebook]. Change your [site configuration] if you would prefer to follow the [Chicago Manual of Style], or to use Go's convention of capitalizing every word. + +[Associated Press (AP) Stylebook]: https://www.apstylebook.com/ +[Chicago Manual of Style]: https://www.chicagomanualofstyle.org/home.html +[site configuration]: /getting-started/configuration/#configure-title-case diff --git a/docs/content/en/functions/strings/ToLower.md b/docs/content/en/functions/strings/ToLower.md new file mode 100644 index 000000000..cb76462ea --- /dev/null +++ b/docs/content/en/functions/strings/ToLower.md @@ -0,0 +1,28 @@ +--- +title: strings.ToLower +linkTitle: lower +description: Converts all characters in the provided string to lowercase. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [lower] + returnType: string + signatures: [strings.ToLower INPUT] +relatedFunctions: + - strings.FirstUpper + - strings.Title + - strings.ToLower + - strings.ToUpper +aliases: [/functions/lower] +--- + + +Note that `lower` can be applied in your templates in more than one way: + +```go-html-template +{{ lower "BatMan" }} → "batman" +{{ "BatMan" | lower }} → "batman" +``` diff --git a/docs/content/en/functions/strings/ToUpper.md b/docs/content/en/functions/strings/ToUpper.md new file mode 100644 index 000000000..d46491637 --- /dev/null +++ b/docs/content/en/functions/strings/ToUpper.md @@ -0,0 +1,27 @@ +--- +title: strings.ToUpper +linkTitle: upper +description: Converts all characters in a string to uppercase +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [upper] + returnType: string + signatures: [strings.ToUpper INPUT] +relatedFunctions: + - strings.FirstUpper + - strings.Title + - strings.ToLower + - strings.ToUpper +aliases: [/functions/upper] +--- + +Note that `upper` can be applied in your templates in more than one way: + +```go-html-template +{{ upper "BatMan" }} → "BATMAN" +{{ "BatMan" | upper }} → "BATMAN" +``` diff --git a/docs/content/en/functions/strings/Trim.md b/docs/content/en/functions/strings/Trim.md new file mode 100644 index 000000000..9eae9ee45 --- /dev/null +++ b/docs/content/en/functions/strings/Trim.md @@ -0,0 +1,45 @@ +--- +title: strings.Trim +linkTitle: trim +description: Returns a slice of a passed string with all leading and trailing characters from cutset removed. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [title] + returnType: string + signatures: [strings.Trim INPUT CUTSET] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/trim] +--- + +```go-html-template +{{ trim "++Batman--" "+-" }} → "Batman" +``` + +`trim` *requires* the second argument, which tells the function specifically what to remove from the first argument. There is no default value for the second argument, so **the following usage will not work**: + +```go-html-template +{{ trim .Inner }} +``` + +Instead, the following example tells `trim` to remove extra new lines from the content contained in the [shortcode `.Inner` variable][shortcodevars]: + +```go-html-template +{{ trim .Inner "\n" }} +``` + +{{% note %}} +Go templates also provide a simple [method for trimming whitespace](/templates/introduction/#whitespace) from either side of a Go tag by including a hyphen (`-`). +{{% /note %}} + + +[shortcodevars]: /variables/shortcodes/ diff --git a/docs/content/en/functions/strings/TrimLeft.md b/docs/content/en/functions/strings/TrimLeft.md new file mode 100644 index 000000000..3924e492f --- /dev/null +++ b/docs/content/en/functions/strings/TrimLeft.md @@ -0,0 +1,33 @@ +--- +title: strings.TrimLeft +description: Returns a slice of a given string with all leading characters contained in the cutset removed. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.TrimLeft CUTSET STRING] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/strings.trimleft] +--- + +Given the string `"abba"`, leading `"a"`'s can be removed a follows: + +```go-html-template +{{ strings.TrimLeft "a" "abba" }} → "bba" +``` + +Numbers can be handled as well: + +```go-html-template +{{ strings.TrimLeft 12 1221341221 }} → "341221" +``` diff --git a/docs/content/en/functions/strings/TrimPrefix.md b/docs/content/en/functions/strings/TrimPrefix.md new file mode 100644 index 000000000..37657732d --- /dev/null +++ b/docs/content/en/functions/strings/TrimPrefix.md @@ -0,0 +1,29 @@ +--- +title: strings.TrimPrefix +description: Returns a given string s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.TrimPrefix PREFIX STRING] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/strings.trimprefix] +--- + +Given the string `"aabbaa"`, the specified prefix is only removed if `"aabbaa"` starts with it: + +```go-html-template +{{ strings.TrimPrefix "a" "aabbaa" }} → "abbaa" +{{ strings.TrimPrefix "aa" "aabbaa" }} → "bbaa" +{{ strings.TrimPrefix "aaa" "aabbaa" }} → "aabbaa" +``` diff --git a/docs/content/en/functions/strings/TrimRight.md b/docs/content/en/functions/strings/TrimRight.md new file mode 100644 index 000000000..fa538b605 --- /dev/null +++ b/docs/content/en/functions/strings/TrimRight.md @@ -0,0 +1,33 @@ +--- +title: strings.TrimRight +description: Returns a slice of a given string with all trailing characters contained in the cutset removed. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.TrimRight CUTSET STRING] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/strings.trimright] +--- + +Given the string `"abba"`, trailing `"a"`'s can be removed a follows: + +```go-html-template +{{ strings.TrimRight "a" "abba" }} → "abb" +``` + +Numbers can be handled as well: + +```go-html-template +{{ strings.TrimRight 12 1221341221 }} → "122134" +``` diff --git a/docs/content/en/functions/strings/TrimSuffix.md b/docs/content/en/functions/strings/TrimSuffix.md new file mode 100644 index 000000000..6dc9becfc --- /dev/null +++ b/docs/content/en/functions/strings/TrimSuffix.md @@ -0,0 +1,29 @@ +--- +title: strings.TrimSuffix +description: Returns a given string s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [] + returnType: string + signatures: [strings.TrimSuffix SUFFIX STRING] +relatedFunctions: + - strings.Chomp + - strings.Trim + - strings.TrimLeft + - strings.TrimPrefix + - strings.TrimRight + - strings.TrimSuffix +aliases: [/functions/strings.trimsuffix] +--- + +Given the string `"aabbaa"`, the specified suffix is only removed if `"aabbaa"` ends with it: + +```go-html-template +{{ strings.TrimSuffix "a" "aabbaa" }} → "aabba" +{{ strings.TrimSuffix "aa" "aabbaa" }} → "aabb" +{{ strings.TrimSuffix "aaa" "aabbaa" }} → "aabbaa" +``` diff --git a/docs/content/en/functions/strings/Truncate.md b/docs/content/en/functions/strings/Truncate.md new file mode 100644 index 000000000..0bd78d840 --- /dev/null +++ b/docs/content/en/functions/strings/Truncate.md @@ -0,0 +1,26 @@ +--- +title: strings.Truncate +linkTitle: truncate +description: Truncates a text to a max length without cutting words or leaving unclosed HTML tags. +categories: [functions] +keywords: [] +menu: + docs: + parent: functions +function: + aliases: [truncate] + returnType: template.HTML + signatures: ['strings.Truncate SIZE [ELLIPSIS] INPUT'] +relatedFunctions: [] +aliases: [/functions/truncate] +--- + +Since Go templates are HTML-aware, `truncate` will intelligently handle normal strings vs HTML strings: + +```go-html-template +{{ "<em>Keep my HTML</em>" | safeHTML | truncate 10 }} → <em>Keep my …</em> +``` + +{{% note %}} +If you have a raw string that contains HTML tags you want to remain treated as HTML, you will need to convert the string to HTML using the [`safeHTML` template function](/functions/safe/html) before sending the value to truncate. Otherwise, the HTML tags will be escaped when passed through the `truncate` function. +{{% /note %}} |