Debian Codegraph @

docs/content/en/functions/strings/Substr.md

raw
1 ---
2 title: strings.Substr
3 description: Returns a substring of the given string, beginning with the start position and ending after the given length.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [substr]
9 returnType: string
10 signatures: ['strings.Substr STRING [START] [LENGTH]']
11 aliases: [/functions/substr]
12 ---
13
14 The start position is zero-based, where `0` represents the first character of the string. If START is not specified, the substring will begin at position `0`. Specify a negative START position to extract characters from the end of the string.
15
16 If LENGTH is not specified, the substring will include all characters from the START position to the end of the string. If negative, that number of characters will be omitted from the end of string.
17
18 ```go-html-template
19 {{ substr "abcdef" 0 }} → abcdef
20 {{ substr "abcdef" 1 }} → bcdef
21
22 {{ substr "abcdef" 0 1 }} → a
23 {{ substr "abcdef" 1 1 }} → b
24
25 {{ substr "abcdef" 0 -1 }} → abcde
26 {{ substr "abcdef" 1 -1 }} → bcde
27
28 {{ substr "abcdef" -1 }} → f
29 {{ substr "abcdef" -2 }} → ef
30
31 {{ substr "abcdef" -1 1 }} → f
32 {{ substr "abcdef" -2 1 }} → e
33
34 {{ substr "abcdef" -3 -1 }} → de
35 {{ substr "abcdef" -3 -2 }} → d
36 ```