mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-28 22:50:35 +03:00
Merge commit '35dec7c96f
'
This commit is contained in:
commit
d19ed4d4e6
810 changed files with 24147 additions and 7766 deletions
|
@ -1,31 +1,27 @@
|
|||
---
|
||||
title: chomp
|
||||
linkTitle: chomp
|
||||
description: Removes any trailing newline characters.
|
||||
categories: [functions]
|
||||
title: strings.Chomp
|
||||
description: Returns the given string, removing all trailing newline characters and carriage returns.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [chomp]
|
||||
related:
|
||||
- functions/strings/Trim
|
||||
- functions/strings/TrimLeft
|
||||
- functions/strings/TrimPrefix
|
||||
- functions/strings/TrimRight
|
||||
- functions/strings/TrimSuffix
|
||||
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)).
|
||||
If the argument is of type `template.HTML`, returns `template.HTML`, else returns a `string`.
|
||||
|
||||
```go-html-template
|
||||
{{ chomp "<p>Blockhead</p>\n" }} → "<p>Blockhead</p>"
|
||||
{{ chomp | "foo\n" }} → foo
|
||||
{{ chomp | "foo\n\n" }} → foo
|
||||
|
||||
{{ chomp | "foo\r\n" }} → foo
|
||||
{{ chomp | "foo\r\n\r\n" }} → foo
|
||||
```
|
||||
|
|
|
@ -1,28 +1,26 @@
|
|||
---
|
||||
title: strings.Contains
|
||||
description: Reports whether the string contains a substring.
|
||||
categories: [functions]
|
||||
description: Reports whether the given string contains the given substring.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/ContainsAny
|
||||
- functions/strings/ContainsNonSpace
|
||||
- functions/strings/HasPrefix
|
||||
- functions/strings/HasSuffix
|
||||
- functions/collections/In
|
||||
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:
|
||||
|
||||
The check is case sensitive:
|
||||
|
||||
```go-html-template
|
||||
{{ strings.Contains "Hugo" "Go" }} → false
|
||||
|
|
|
@ -1,21 +1,18 @@
|
|||
---
|
||||
title: strings.ContainsAny
|
||||
description: Reports whether a string contains any character from a given string.
|
||||
categories: [functions]
|
||||
description: Reports whether the given string contains any character within the given set.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Contains
|
||||
- functions/strings/ContainsNonSpace
|
||||
- functions/strings/HasPrefix
|
||||
- functions/strings/HasSuffix
|
||||
- functions/collections/In
|
||||
returnType: bool
|
||||
signatures: [strings.ContainsAny STRING CHARACTERS]
|
||||
relatedFunctions:
|
||||
- strings.Contains
|
||||
- strings.ContainsAny
|
||||
- strings.ContainsNonSpace
|
||||
- strings.HasPrefix
|
||||
- strings.HasSuffix
|
||||
signatures: [strings.ContainsAny STRING SET]
|
||||
aliases: [/functions/strings.containsany]
|
||||
---
|
||||
|
||||
|
@ -23,7 +20,7 @@ aliases: [/functions/strings.containsany]
|
|||
{{ strings.ContainsAny "Hugo" "gm" }} → true
|
||||
```
|
||||
|
||||
The check is case sensitive:
|
||||
The check is case sensitive:
|
||||
|
||||
```go-html-template
|
||||
{{ strings.ContainsAny "Hugo" "Gm" }} → false
|
||||
|
|
|
@ -1,27 +1,26 @@
|
|||
---
|
||||
title: strings.ContainsNonSpace
|
||||
description: Reports whether a string contains any non-space characters as defined by Unicode’s White Space property.
|
||||
categories: [functions]
|
||||
description: Reports whether the given string contains any non-space characters as defined by Unicode’s White Space property.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Contains
|
||||
- functions/strings/ContainsAny
|
||||
- functions/strings/HasPrefix
|
||||
- functions/strings/HasSuffix
|
||||
- functions/collections/In
|
||||
returnType: bool
|
||||
signatures: [strings.ContainsNonSpace STRING]
|
||||
relatedFunctions:
|
||||
- strings.Contains
|
||||
- strings.ContainsAny
|
||||
- strings.ContainsNonSpace
|
||||
- strings.HasPrefix
|
||||
- strings.HasSuffix
|
||||
aliases: [/functions/strings.containsnonspace]
|
||||
---
|
||||
|
||||
{{< new-in 0.111.0 >}}
|
||||
|
||||
```go-html-template
|
||||
{{ strings.ContainsNonSpace "\n" }} → false
|
||||
{{ strings.ContainsNonSpace " " }} → false
|
||||
{{ strings.ContainsNonSpace "\n" }} → false
|
||||
{{ strings.ContainsNonSpace " " }} → false
|
||||
{{ strings.ContainsNonSpace "\n abc" }} → true
|
||||
```
|
||||
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
---
|
||||
title: strings.Count
|
||||
description: Returns the number of non-overlapping instances of a substring within a string.
|
||||
categories: [functions]
|
||||
description: Returns the number of non-overlapping instances of the given substring within the given string.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/go-template/len
|
||||
- functions/strings/CountRunes
|
||||
- functions/strings/CountWords
|
||||
- functions/strings/RuneCount
|
||||
returnType: int
|
||||
signatures: [strings.Count SUBSTR STRING]
|
||||
relatedFunctions:
|
||||
- len
|
||||
- strings.Count
|
||||
- strings.CountRunes
|
||||
- strings.CountWords
|
||||
- strings.RuneCount
|
||||
aliases: [/functions/strings.count]
|
||||
---
|
||||
|
||||
|
|
|
@ -1,22 +1,17 @@
|
|||
---
|
||||
title: strings.CountRunes
|
||||
linkTitle: countrunes
|
||||
description: Returns the number of runes in a string excluding whitespace.
|
||||
categories: [functions]
|
||||
description: Returns the number of runes in the given string excluding whitespace.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [countrunes]
|
||||
related:
|
||||
- functions/go-template/len
|
||||
- functions/strings/Count
|
||||
- functions/strings/CountWords
|
||||
- functions/strings/RuneCount
|
||||
returnType: int
|
||||
signatures: [strings.CountRunes INPUT]
|
||||
relatedFunctions:
|
||||
- len
|
||||
- strings.Count
|
||||
- strings.CountRunes
|
||||
- strings.CountWords
|
||||
- strings.RuneCount
|
||||
aliases: [/functions/countrunes]
|
||||
---
|
||||
|
||||
|
|
|
@ -1,31 +1,20 @@
|
|||
---
|
||||
title: strings.CountWords
|
||||
linkTitle: countwords
|
||||
description: Counts the number of words in a string.
|
||||
categories: [functions]
|
||||
description: Returns the number of words in the given string.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [countwords]
|
||||
related:
|
||||
- functions/go-template/len
|
||||
- functions/strings/Count
|
||||
- functions/strings/CountRunes
|
||||
- functions/strings/RuneCount
|
||||
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. -->
|
||||
{{ "Hugo is a static site generator." | countwords }} → 6
|
||||
```
|
||||
|
||||
|
||||
[pagevars]: /variables/page/
|
||||
|
|
|
@ -1,27 +1,22 @@
|
|||
---
|
||||
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]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [findRESubmatch]
|
||||
related:
|
||||
- functions/strings/FindRE
|
||||
- functions/strings/Replace
|
||||
- functions/strings/ReplaceRE
|
||||
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" %}}
|
||||
{{% include "functions/_common/regular-expressions.md" %}}
|
||||
|
||||
## Demonstrative examples
|
||||
|
||||
|
|
|
@ -1,26 +1,21 @@
|
|||
---
|
||||
title: strings.FindRE
|
||||
linkTitle: findRE
|
||||
description: Returns a slice of strings that match the regular expression.
|
||||
categories: [functions]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [findRE]
|
||||
related:
|
||||
- functions/strings/FindRESubmatch
|
||||
- functions/strings/Replace
|
||||
- functions/strings/ReplaceRE
|
||||
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" %}}
|
||||
{{% include "functions/_common/regular-expressions.md" %}}
|
||||
|
||||
This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`:
|
||||
|
||||
|
|
|
@ -1,23 +1,19 @@
|
|||
---
|
||||
title: strings.FirstUpper
|
||||
description: Capitalizes the first character of a given string.
|
||||
categories: [functions]
|
||||
description: Returns the given string, capitalizing the first character.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Title
|
||||
- functions/strings/ToLower
|
||||
- functions/strings/ToUpper
|
||||
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"
|
||||
{{ strings.FirstUpper "foo" }} → Foo
|
||||
```
|
||||
|
|
|
@ -1,21 +1,18 @@
|
|||
---
|
||||
title: strings.HasPrefix
|
||||
description: Reports whether a string begins with prefix.
|
||||
categories: [functions]
|
||||
description: Reports whether the given string begins with the given prefix.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [hasPrefix]
|
||||
related:
|
||||
- functions/strings/Contains
|
||||
- functions/strings/ContainsAny
|
||||
- functions/strings/ContainsNonSpace
|
||||
- functions/strings/HasSuffix
|
||||
- functions/collections/In
|
||||
returnType: bool
|
||||
signatures: [strings.HasPrefix STRING PREFIX]
|
||||
relatedFunctions:
|
||||
- strings.Contains
|
||||
- strings.ContainsAny
|
||||
- strings.ContainsNonSpace
|
||||
- strings.HasPrefix
|
||||
- strings.HasSuffix
|
||||
aliases: [/functions/hasprefix,/functions/strings.hasprefix]
|
||||
---
|
||||
|
||||
|
|
|
@ -1,21 +1,18 @@
|
|||
---
|
||||
title: strings.HasSuffix
|
||||
description: Reports whether a string ends with suffix.
|
||||
categories: [functions]
|
||||
description: Reports whether the given string begins with the given suffix.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [hasSuffix]
|
||||
related:
|
||||
- functions/strings/Contains
|
||||
- functions/strings/ContainsAny
|
||||
- functions/strings/ContainsNonSpace
|
||||
- functions/strings/HasPrefix
|
||||
- functions/collections/In
|
||||
returnType: bool
|
||||
signatures: [strings.HasSuffix STRING SUFFIX]
|
||||
relatedFunctions:
|
||||
- strings.Contains
|
||||
- strings.ContainsAny
|
||||
- strings.ContainsNonSpace
|
||||
- strings.HasPrefix
|
||||
- strings.HasSuffix
|
||||
aliases: [/functions/hassuffix,/functions/strings/hassuffix]
|
||||
---
|
||||
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
---
|
||||
title: strings.Repeat
|
||||
description: Returns a new string consisting of zero or more copies of another string.
|
||||
categories: [functions]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related: []
|
||||
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"
|
||||
{{ strings.Repeat 3 "yo" }} → yoyoyo
|
||||
```
|
||||
|
|
|
@ -1,30 +1,26 @@
|
|||
---
|
||||
title: strings.Replace
|
||||
linkTitle: replace
|
||||
description: Replaces all occurrences of the search string with the replacement string.
|
||||
categories: [functions]
|
||||
description: Returns a copy of INPUT, replacing all occurrences of OLD with NEW.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [replace]
|
||||
related:
|
||||
- functions/strings/FindRE
|
||||
- functions/strings/FindRESubmatch
|
||||
- functions/strings/ReplaceRE
|
||||
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.
|
||||
|
||||
```go-html-template
|
||||
{{ $s := "Batman and Robin" }}
|
||||
{{ replace $s "Robin" "Catwoman" }} → Batman and Catwoman
|
||||
```
|
||||
{{ replace "Batman and Robin" "Robin" "Catwoman" }}
|
||||
→ "Batman and Catwoman"
|
||||
|
||||
{{ replace "aabbaabb" "a" "z" 2 }} → "zzbbaabb"
|
||||
Limit the number of replacements using the `LIMIT` argument:
|
||||
|
||||
```go-html-template
|
||||
{{ replace "aabbaabb" "a" "z" 2 }} → zzbbaabb
|
||||
```
|
||||
|
|
|
@ -1,42 +1,34 @@
|
|||
---
|
||||
title: strings.ReplaceRE
|
||||
linkTitle: replaceRE
|
||||
description: Returns a string, replacing all occurrences of a regular expression with a replacement pattern.
|
||||
categories: [functions]
|
||||
description: Returns a copy of INPUT, replacing all occurrences of a regular expression with a replacement pattern.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [replaceRE]
|
||||
related:
|
||||
- functions/strings/FindRE
|
||||
- functions/strings/FindRESubmatch
|
||||
- functions/strings/Replace
|
||||
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:
|
||||
{{% include "functions/_common/regular-expressions.md" %}}
|
||||
|
||||
```go-html-template
|
||||
{{ $s := "a-b--c---d" }}
|
||||
{{ replaceRE `(-{2,})` "-" $s }} → a-b-c-d
|
||||
```
|
||||
|
||||
To limit the number of replacements to one:
|
||||
Limit the number of replacements using the LIMIT argument:
|
||||
|
||||
```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:
|
||||
Use `$1`, `$2`, etc. within the replacement string to insert the content of each capturing group within the regular expression:
|
||||
|
||||
```go-html-template
|
||||
{{ $s := "http://gohugo.io/docs" }}
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
---
|
||||
title: strings.RuneCount
|
||||
description: Returns the number of runes in a string.
|
||||
categories: [functions]
|
||||
description: Returns the number of runes in the given string.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/go-template/len
|
||||
- functions/strings/Count
|
||||
- functions/strings/CountRunes
|
||||
- functions/strings/CountWords
|
||||
returnType: int
|
||||
signatures: [strings.RuneCount INPUT]
|
||||
relatedFunctions:
|
||||
- len
|
||||
- strings.Count
|
||||
- strings.CountRunes
|
||||
- strings.CountWords
|
||||
- strings.RuneCount
|
||||
aliases: [/functions/strings.runecount]
|
||||
---
|
||||
|
||||
|
|
|
@ -1,24 +1,20 @@
|
|||
---
|
||||
title: strings.SliceString
|
||||
linkTitle: slicestr
|
||||
description: Creates a slice of a half-open range, including start and end indices.
|
||||
categories: [functions]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [slicestr]
|
||||
related: []
|
||||
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.
|
||||
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"
|
||||
{{ slicestr "BatMan" 3 }}` → Man
|
||||
{{ slicestr "BatMan" 0 3 }}` → Bat
|
||||
```
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
---
|
||||
title: strings.Split
|
||||
linkTitle: split
|
||||
description: Returns a slice of strings by splitting STRING by DELIM.
|
||||
categories: [functions]
|
||||
description: Returns a slice of strings by splitting the given string by a delimiter.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [split]
|
||||
related:
|
||||
- functions/collections/Delimit
|
||||
returnType: string
|
||||
signatures: [strings.Split STRING DELIM]
|
||||
relatedFunctions:
|
||||
- collections.Delimit
|
||||
- strings.Split
|
||||
aliases: [/functions/split]
|
||||
---
|
||||
|
||||
|
@ -24,7 +19,8 @@ Examples:
|
|||
{{ 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.
|
||||
The `strings.Split` function essentially does the opposite of the [`collections.Delimit`] function. While `split` creates a slice from a string, `delimit` creates a string from a slice.
|
||||
|
||||
[`collections.Delimit`]: /functions/collections/delimit
|
||||
{{% /note %}}
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
---
|
||||
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]
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [substr]
|
||||
related: []
|
||||
returnType: string
|
||||
signatures: ['strings.Substr STRING START [LENGTH]']
|
||||
relatedFunctions: []
|
||||
aliases: [/functions/substr]
|
||||
---
|
||||
|
||||
|
@ -22,21 +18,21 @@ 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 }} → abcdef
|
||||
{{ substr "abcdef" 1 }} → bcdef
|
||||
|
||||
{{ substr "abcdef" 0 1 }} → "a"
|
||||
{{ substr "abcdef" 1 1 }} → "b"
|
||||
{{ substr "abcdef" 0 1 }} → a
|
||||
{{ substr "abcdef" 1 1 }} → b
|
||||
|
||||
{{ substr "abcdef" 0 -1 }} → "abcde"
|
||||
{{ substr "abcdef" 1 -1 }} → "bcde"
|
||||
{{ substr "abcdef" 0 -1 }} → abcde
|
||||
{{ substr "abcdef" 1 -1 }} → bcde
|
||||
|
||||
{{ substr "abcdef" -1 }} → "f"
|
||||
{{ substr "abcdef" -2 }} → "ef"
|
||||
{{ substr "abcdef" -1 }} → f
|
||||
{{ substr "abcdef" -2 }} → ef
|
||||
|
||||
{{ substr "abcdef" -1 1 }} → "f"
|
||||
{{ substr "abcdef" -2 1 }} → "e"
|
||||
{{ substr "abcdef" -1 1 }} → f
|
||||
{{ substr "abcdef" -2 1 }} → e
|
||||
|
||||
{{ substr "abcdef" -3 -1 }} → "de"
|
||||
{{ substr "abcdef" -3 -2 }} → "d"
|
||||
{{ substr "abcdef" -3 -1 }} → de
|
||||
{{ substr "abcdef" -3 -2 }} → d
|
||||
```
|
||||
|
|
|
@ -1,30 +1,32 @@
|
|||
---
|
||||
title: strings.Title
|
||||
linkTitle: title
|
||||
description: Converts the provided string to title case.
|
||||
categories: [functions]
|
||||
description: Returns the given string, converting it to title case.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [title]
|
||||
related:
|
||||
- functions/strings/FirstUpper
|
||||
- functions/strings/ToLower
|
||||
- functions/strings/ToUpper
|
||||
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)"
|
||||
{{ 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.
|
||||
By default, Hugo follows the capitalization rules published in the [Associated Press Stylebook]. Change your [site configuration] if you would prefer to:
|
||||
|
||||
[Associated Press (AP) Stylebook]: https://www.apstylebook.com/
|
||||
- Follow the capitalization rules published in the [Chicago Manual of Style]
|
||||
- Capitalize the first letter of every word
|
||||
- Capitalize the first letter of the first word
|
||||
- Disable the effects of the `title` function
|
||||
|
||||
The last option is useful if your theme uses the `title` function, and you would prefer to manually capitalize strings as needed.
|
||||
|
||||
[Associated Press Stylebook]: https://www.apstylebook.com/
|
||||
[Chicago Manual of Style]: https://www.chicagomanualofstyle.org/home.html
|
||||
[site configuration]: /getting-started/configuration/#configure-title-case
|
||||
|
|
|
@ -1,28 +1,19 @@
|
|||
---
|
||||
title: strings.ToLower
|
||||
linkTitle: lower
|
||||
description: Converts all characters in the provided string to lowercase.
|
||||
categories: [functions]
|
||||
description: Returns the given string, converting all characters to lowercase.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [lower]
|
||||
related:
|
||||
- functions/strings/FirstUpper
|
||||
- functions/strings/Title
|
||||
- functions/strings/ToUpper
|
||||
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"
|
||||
{{ lower "BatMan" }} → batman
|
||||
```
|
||||
|
|
|
@ -1,27 +1,19 @@
|
|||
---
|
||||
title: strings.ToUpper
|
||||
linkTitle: upper
|
||||
description: Converts all characters in a string to uppercase
|
||||
categories: [functions]
|
||||
description: Returns the given string, converting all characters to uppercase.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [upper]
|
||||
related:
|
||||
- functions/strings/FirstUpper
|
||||
- functions/strings/Title
|
||||
- functions/strings/ToLower
|
||||
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"
|
||||
{{ upper "BatMan" }} → BATMAN
|
||||
```
|
||||
|
|
|
@ -1,45 +1,59 @@
|
|||
---
|
||||
title: strings.Trim
|
||||
linkTitle: trim
|
||||
description: Returns a slice of a passed string with all leading and trailing characters from cutset removed.
|
||||
categories: [functions]
|
||||
description: Returns the given string, removing leading and trailing characters specified in the cutset.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
aliases: [title]
|
||||
action:
|
||||
aliases: [trim]
|
||||
related:
|
||||
- functions/strings/Chomp
|
||||
- functions/strings/TrimLeft
|
||||
- functions/strings/TrimPrefix
|
||||
- functions/strings/TrimRight
|
||||
- functions/strings/TrimSuffix
|
||||
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 "++foo--" "+-" }} → foo
|
||||
```
|
||||
|
||||
`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**:
|
||||
To remove leading and trailing newline characters and carriage returns:
|
||||
|
||||
```go-html-template
|
||||
{{ trim .Inner }}
|
||||
{{ trim "\nfoo\n" "\n\r" }} → foo
|
||||
{{ trim "\n\nfoo\n\n" "\n\r" }} → foo
|
||||
|
||||
{{ trim "\r\nfoo\r\n" "\n\r" }} → foo
|
||||
{{ trim "\r\n\r\nfoo\r\n\r\n" "\n\r" }} → foo
|
||||
```
|
||||
|
||||
Instead, the following example tells `trim` to remove extra new lines from the content contained in the [shortcode `.Inner` variable][shortcodevars]:
|
||||
The `strings.Trim` function is commonly used in shortcodes to remove leading and trailing newlines characters and carriage returns from the content within the opening and closing shortcode tags.
|
||||
|
||||
For example, with this markdown:
|
||||
|
||||
```text
|
||||
{{</* my-shortcode */>}}
|
||||
Able was I ere I saw Elba.
|
||||
{{</* /my-shortcode */>}}
|
||||
```
|
||||
|
||||
The value of `.Inner` in the shortcode template is:
|
||||
|
||||
```text
|
||||
\nAble was I ere I saw Elba.\n
|
||||
```
|
||||
|
||||
If authored on a Windows system the value of `.Inner` might, depending on the editor configuration, be:
|
||||
|
||||
```text
|
||||
\r\nAble was I ere I saw Elba.\r\n
|
||||
```
|
||||
|
||||
This construct is common in shortcode templates:
|
||||
|
||||
```go-html-template
|
||||
{{ trim .Inner "\n" }}
|
||||
{{ trim .Inner "\n\r" }}
|
||||
```
|
||||
|
||||
{{% 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/
|
||||
|
|
|
@ -1,33 +1,28 @@
|
|||
---
|
||||
title: strings.TrimLeft
|
||||
description: Returns a slice of a given string with all leading characters contained in the cutset removed.
|
||||
categories: [functions]
|
||||
description: Returns the given string, removing leading characters specified in the cutset.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Chomp
|
||||
- functions/strings/Trim
|
||||
- functions/strings/TrimPrefix
|
||||
- functions/strings/TrimRight
|
||||
- functions/strings/TrimSuffix
|
||||
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"
|
||||
{{ strings.TrimLeft "a" "abba" }} → bba
|
||||
```
|
||||
|
||||
Numbers can be handled as well:
|
||||
The `strings.TrimLeft` function converts the arguments to strings if possible:
|
||||
|
||||
```go-html-template
|
||||
{{ strings.TrimLeft 12 1221341221 }} → "341221"
|
||||
{{ strings.TrimLeft 21 12345 }} → 345 (string)
|
||||
{{ strings.TrimLeft "rt" true }} → ue
|
||||
```
|
||||
|
|
|
@ -1,29 +1,23 @@
|
|||
---
|
||||
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]
|
||||
description: Returns the given string, removing the prefix from the beginning of the string.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Chomp
|
||||
- functions/strings/Trim
|
||||
- functions/strings/TrimLeft
|
||||
- functions/strings/TrimRight
|
||||
- functions/strings/TrimSuffix
|
||||
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"
|
||||
{{ strings.TrimPrefix "a" "aabbaa" }} → abbaa
|
||||
{{ strings.TrimPrefix "aa" "aabbaa" }} → bbaa
|
||||
{{ strings.TrimPrefix "aaa" "aabbaa" }} → aabbaa
|
||||
```
|
||||
|
|
|
@ -1,33 +1,28 @@
|
|||
---
|
||||
title: strings.TrimRight
|
||||
description: Returns a slice of a given string with all trailing characters contained in the cutset removed.
|
||||
categories: [functions]
|
||||
description: Returns the given string, removing trailing characters specified in the cutset.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Chomp
|
||||
- functions/strings/Trim
|
||||
- functions/strings/TrimLeft
|
||||
- functions/strings/TrimPrefix
|
||||
- functions/strings/TrimSuffix
|
||||
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"
|
||||
{{ strings.TrimRight "a" "abba" }} → abb
|
||||
```
|
||||
|
||||
Numbers can be handled as well:
|
||||
The `strings.TrimRight` function converts the arguments to strings if possible:
|
||||
|
||||
```go-html-template
|
||||
{{ strings.TrimRight 12 1221341221 }} → "122134"
|
||||
{{ strings.TrimRight 54 12345 }} → 123 (string)
|
||||
{{ strings.TrimRight "eu" true }} → tr
|
||||
```
|
||||
|
|
|
@ -1,29 +1,23 @@
|
|||
---
|
||||
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]
|
||||
description: Returns the given string, removing the suffix from the end of the string.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/strings/Chomp
|
||||
- functions/strings/Trim
|
||||
- functions/strings/TrimLeft
|
||||
- functions/strings/TrimPrefix
|
||||
- functions/strings/TrimRight
|
||||
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"
|
||||
{{ strings.TrimSuffix "a" "aabbaa" }} → aabba
|
||||
{{ strings.TrimSuffix "aa" "aabbaa" }} → aabb
|
||||
{{ strings.TrimSuffix "aaa" "aabbaa" }} → aabbaa
|
||||
```
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
---
|
||||
title: strings.Truncate
|
||||
linkTitle: truncate
|
||||
description: Truncates a text to a max length without cutting words or leaving unclosed HTML tags.
|
||||
categories: [functions]
|
||||
description: Returns the given string, truncating it to a maximum length without cutting words or leaving unclosed HTML tags.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
action:
|
||||
aliases: [truncate]
|
||||
related: []
|
||||
returnType: template.HTML
|
||||
signatures: ['strings.Truncate SIZE [ELLIPSIS] INPUT']
|
||||
relatedFunctions: []
|
||||
aliases: [/functions/truncate]
|
||||
---
|
||||
|
||||
|
@ -22,5 +18,7 @@ Since Go templates are HTML-aware, `truncate` will intelligently handle normal s
|
|||
```
|
||||
|
||||
{{% 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.
|
||||
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`]function before sending the value to `truncate`. Otherwise, the HTML tags will be escaped when passed through the `truncate` function.
|
||||
|
||||
[`safeHTML`]: /functions/safe/html
|
||||
{{% /note %}}
|
||||
|
|
12
docs/content/en/functions/strings/_index.md
Normal file
12
docs/content/en/functions/strings/_index.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
title: String functions
|
||||
linkTitle: strings
|
||||
description: Template functions to work with strings.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
---
|
||||
|
||||
Use these functions to work with strings.
|
Loading…
Add table
Add a link
Reference in a new issue