mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'
This commit is contained in:
31
docs/content/en/functions/strings/Chomp.md
Normal file
31
docs/content/en/functions/strings/Chomp.md
Normal file
@@ -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>"
|
||||
```
|
29
docs/content/en/functions/strings/Contains.md
Normal file
29
docs/content/en/functions/strings/Contains.md
Normal file
@@ -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
|
||||
```
|
30
docs/content/en/functions/strings/ContainsAny.md
Normal file
30
docs/content/en/functions/strings/ContainsAny.md
Normal file
@@ -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
|
||||
```
|
36
docs/content/en/functions/strings/ContainsNonSpace.md
Normal file
36
docs/content/en/functions/strings/ContainsNonSpace.md
Normal file
@@ -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
|
29
docs/content/en/functions/strings/Count.md
Normal file
29
docs/content/en/functions/strings/Count.md
Normal file
@@ -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
|
||||
```
|
29
docs/content/en/functions/strings/CountRunes.md
Normal file
29
docs/content/en/functions/strings/CountRunes.md
Normal file
@@ -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
|
31
docs/content/en/functions/strings/CountWords.md
Normal file
31
docs/content/en/functions/strings/CountWords.md
Normal file
@@ -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/
|
95
docs/content/en/functions/strings/FindRESubmatch.md
Normal file
95
docs/content/en/functions/strings/FindRESubmatch.md
Normal file
@@ -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 %}}
|
41
docs/content/en/functions/strings/FindRe.md
Normal file
41
docs/content/en/functions/strings/FindRe.md
Normal file
@@ -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 %}}
|
23
docs/content/en/functions/strings/FirstUpper.md
Normal file
23
docs/content/en/functions/strings/FirstUpper.md
Normal file
@@ -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"
|
||||
```
|
24
docs/content/en/functions/strings/HasPrefix.md
Normal file
24
docs/content/en/functions/strings/HasPrefix.md
Normal file
@@ -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
|
||||
```
|
24
docs/content/en/functions/strings/HasSuffix.md
Normal file
24
docs/content/en/functions/strings/HasSuffix.md
Normal file
@@ -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
|
||||
```
|
20
docs/content/en/functions/strings/Repeat.md
Normal file
20
docs/content/en/functions/strings/Repeat.md
Normal file
@@ -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"
|
||||
```
|
30
docs/content/en/functions/strings/Replace.md
Normal file
30
docs/content/en/functions/strings/Replace.md
Normal file
@@ -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"
|
||||
```
|
51
docs/content/en/functions/strings/ReplaceRE.md
Normal file
51
docs/content/en/functions/strings/ReplaceRE.md
Normal file
@@ -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
|
28
docs/content/en/functions/strings/RuneCount.md
Normal file
28
docs/content/en/functions/strings/RuneCount.md
Normal file
@@ -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
|
24
docs/content/en/functions/strings/SliceString.md
Normal file
24
docs/content/en/functions/strings/SliceString.md
Normal file
@@ -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"
|
||||
```
|
30
docs/content/en/functions/strings/Split.md
Normal file
30
docs/content/en/functions/strings/Split.md
Normal file
@@ -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 %}}
|
42
docs/content/en/functions/strings/Substr.md
Normal file
42
docs/content/en/functions/strings/Substr.md
Normal file
@@ -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"
|
||||
```
|
30
docs/content/en/functions/strings/Title.md
Normal file
30
docs/content/en/functions/strings/Title.md
Normal file
@@ -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
|
28
docs/content/en/functions/strings/ToLower.md
Normal file
28
docs/content/en/functions/strings/ToLower.md
Normal file
@@ -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"
|
||||
```
|
27
docs/content/en/functions/strings/ToUpper.md
Normal file
27
docs/content/en/functions/strings/ToUpper.md
Normal file
@@ -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"
|
||||
```
|
45
docs/content/en/functions/strings/Trim.md
Normal file
45
docs/content/en/functions/strings/Trim.md
Normal file
@@ -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/
|
33
docs/content/en/functions/strings/TrimLeft.md
Normal file
33
docs/content/en/functions/strings/TrimLeft.md
Normal file
@@ -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"
|
||||
```
|
29
docs/content/en/functions/strings/TrimPrefix.md
Normal file
29
docs/content/en/functions/strings/TrimPrefix.md
Normal file
@@ -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"
|
||||
```
|
33
docs/content/en/functions/strings/TrimRight.md
Normal file
33
docs/content/en/functions/strings/TrimRight.md
Normal file
@@ -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"
|
||||
```
|
29
docs/content/en/functions/strings/TrimSuffix.md
Normal file
29
docs/content/en/functions/strings/TrimSuffix.md
Normal file
@@ -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"
|
||||
```
|
26
docs/content/en/functions/strings/Truncate.md
Normal file
26
docs/content/en/functions/strings/Truncate.md
Normal file
@@ -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 %}}
|
Reference in New Issue
Block a user