2019-10-21 04:22:28 -04:00
---
2023-10-20 03:42:39 -04:00
title: strings.Trim
2023-12-04 09:14:18 -05:00
description: Returns the given string, removing leading and trailing characters specified in the cutset.
categories: []
2023-10-20 03:42:39 -04:00
keywords: []
2023-12-04 09:14:18 -05:00
action:
aliases: [trim]
related:
- functions/strings/Chomp
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
2023-10-20 03:42:39 -04:00
returnType: string
signatures: [strings.Trim INPUT CUTSET]
aliases: [/functions/trim]
2019-10-21 04:22:28 -04:00
---
2023-05-22 10:43:12 -04:00
```go-html-template
2023-12-04 09:14:18 -05:00
{{ trim "++foo--" "+-" }} → foo
2019-10-21 04:22:28 -04:00
```
2023-12-04 09:14:18 -05:00
To remove leading and trailing newline characters and carriage returns:
2019-10-21 04:22:28 -04:00
2023-05-22 10:43:12 -04:00
```go-html-template
2023-12-04 09:14:18 -05:00
{{ 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
2019-10-21 04:22:28 -04:00
```
2023-12-04 09:14:18 -05:00
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.
2019-10-21 04:22:28 -04:00
2023-12-04 09:14:18 -05:00
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
2019-10-21 04:22:28 -04:00
```
2023-12-04 09:14:18 -05:00
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
```
2019-10-21 04:22:28 -04:00
2023-12-04 09:14:18 -05:00
This construct is common in shortcode templates:
2019-10-21 04:22:28 -04:00
2023-12-04 09:14:18 -05:00
```go-html-template
{{ trim .Inner "\n\r" }}
```