substr.md (1265B)
1 --- 2 title: substr 3 # linktitle: 4 description: Extracts parts of a string from a specified character's position and returns the specified number of characters. 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [functions] 9 menu: 10 docs: 11 parent: "functions" 12 keywords: [strings] 13 aliases: [] 14 signature: ["substr STRING START [LENGTH]"] 15 workson: [] 16 hugoversion: 17 relatedfuncs: [] 18 deprecated: false 19 --- 20 21 It normally takes two parameters: `start` and `length`. It can also take one parameter: `start`, i.e. `length` is omitted, in which case the substring starting from start until the end of the string will be returned. 22 23 To extract characters from the end of the string, use a negative start number. 24 25 If `length` is given and is negative, that number of characters will be omitted from the end of string. 26 27 ``` 28 {{ substr "abcdef" 0 }} → "abcdef" 29 {{ substr "abcdef" 1 }} → "bcdef" 30 31 {{ substr "abcdef" 0 1 }} → "a" 32 {{ substr "abcdef" 1 1 }} → "b" 33 34 {{ substr "abcdef" 0 -1 }} → "abcde" 35 {{ substr "abcdef" 1 -1 }} → "bcde" 36 37 {{ substr "abcdef" -1 }} → "f" 38 {{ substr "abcdef" -2 }} → "ef" 39 40 {{ substr "abcdef" -1 1 }} → "f" 41 {{ substr "abcdef" -2 1 }} → "e" 42 43 {{ substr "abcdef" -3 -1 }} → "de" 44 {{ substr "abcdef" -3 -2 }} → "d" 45 ```