2017-04-05 18:24:40 -05:00

38 lines
1.1 KiB
Markdown

---
title: trim
linktitle:
description: Returns a slice of a passed string with all leading and trailing characters from cutset removed.
godocref:
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [functions]
tags: [strings]
ns:
signature: ["trim INPUT CUTSET"]
workson: []
hugoversion:
relatedfuncs: []
deprecated: false
---
`trim` returns a slice of the string with all leading and trailing characters contained in cutset removed.
```
{{ 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**:
```
{{ trim .Inner}}
```
Instead, the following example tells `trim` to remove extra new lines from the content contained in the `.Inner`, which has specific usage with [shortcodes](/templates/shortcode-templates/):
```
{{ trim .Inner "\n" }}
```
Go templates also provide a simple [method for trimming whitespace](/templates/introduction/#whitespace) from either side of a Go tag by including a hyphen (`-`).