strings functions

strings.Contains

Reports whether a substring is contained within a string.

Usage

strings.Contains substr input
input | strings.Contains substr

Example

input.tmpl:

{{ if (.Env.FOO | strings.Contains "f") }}yes{{else}}no{{end}}
$ FOO=foo gomplate < input.tmpl
yes
$ FOO=bar gomplate < input.tmpl
no

strings.HasPrefix

Tests whether a string begins with a certain prefix.

Usage

strings.HasPrefix prefix input
input | strings.HasPrefix prefix

Example

$ URL=http://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
bar
$ URL=https://example.com gomplate -i '{{if .Env.URL | strings.HasPrefix "https"}}foo{{else}}bar{{end}}'
foo

strings.HasSuffix

Tests whether a string ends with a certain suffix.

Usage

strings.HasSuffix suffix input
input | strings.HasSuffix suffix

Examples

input.tmpl:

{{.Env.URL}}{{if not (.Env.URL | strings.HasSuffix ":80")}}:80{{end}}
$ URL=http://example.com gomplate < input.tmpl
http://example.com:80

strings.Indent

Alias: indent

Indents a string. If the input string has multiple lines, each line will be indented.

Usage

strings.Indent [width] [indent] input
input | strings.Indent [width] [indent]

Arguments

name description
width (optional) number of times to repeat the indent string. Default: 1
indent (optional) the string to indent with. Default: " "
input the string to indent

Example

This function can be especially useful when adding YAML snippets into other YAML documents, where indentation is important:

input.tmpl:

foo:
{{ `{"bar": {"baz": 2}}` | json | toYAML | strings.Indent "  " }}
{{- `{"qux": true}` | json | toYAML | strings.Indent 2 }}
  quux:
{{ `{"quuz": 42}` | json | toYAML | strings.Indent 2 "  " -}}
$ gomplate -f input.tmpl
foo:
  bar:
    baz: 2
  qux: true

  quux: 
    quuz: 42

strings.Split

Creates a slice by splitting a string on a given delimiter.

Usage

strings.Split separator input
input | strings.Split separator

Examples

$ gomplate -i '{{range ("Bart,Lisa,Maggie" | strings.Split ",") }}Hello, {{.}}{{end}}'
Hello, Bart
Hello, Lisa
Hello, Maggie

strings.SplitN

Creates a slice by splitting a string on a given delimiter. The count determines the number of substrings to return.

Usage

strings.SplitN separator count input
input | strings.SplitN separator count

Example

$ gomplate -i '{{ range ("foo:bar:baz" | strings.SplitN ":" 2) }}{{.}}{{end}}'
foo
bar:baz

strings.ReplaceAll

Alias: replaceAll

Replaces all occurrences of a given string with another.

Usage

strings.ReplaceAll old new input
input | strings.ReplaceAll old new

Examples

$ gomplate -i '{{ strings.ReplaceAll "." "-" "172.21.1.42" }}'
172-21-1-42
$ gomplate -i '{{ "172.21.1.42" | strings.ReplaceAll "." "-" }}'
172-21-1-42

strings.Title

Alias: title

Convert to title-case.

Usage

strings.Title input
input | strings.Title

Example

$ gomplate -i '{{strings.Title "hello, world!"}}'
Hello, World!

strings.ToLower

Alias: toLower

Convert to lower-case.

Usage

strings.ToLower input
input | strings.ToLower

Example

$ echo '{{strings.ToLower "HELLO, WORLD!"}}' | gomplate
hello, world!

strings.ToUpper

Alias: toUpper

Convert to upper-case.

Usage

strings.ToUpper input
input | strings.ToUpper

Example

$ gomplate -i '{{strings.ToUpper "hello, world!"}}'
HELLO, WORLD!

strings.Trim

Trims a string by removing the given characters from the beginning and end of the string.

Usage

strings.Trim cutset input
input | strings.Trim cutset

Example

$ gomplate -i '{{ "_-foo-_" | strings.Trim "_-" }}
foo

strings.TrimSpace

Alias: trimSpace

Trims a string by removing whitespace from the beginning and end of the string.

Usage

strings.TrimSpace input
input | strings.TrimSpace

Example

$ gomplate -i '{{ "  \n\t foo" | strings.TrimSpace }}'
foo

contains

See `strings.Contains for a pipeline-compatible version

Contains reports whether the second string is contained within the first. Equivalent to strings.Contains

Example

input.tmpl:

{{if contains .Env.FOO "f"}}yes{{else}}no{{end}}
$ FOO=foo gomplate < input.tmpl
yes
$ FOO=bar gomplate < input.tmpl
no

hasPrefix

See `strings.HasPrefix for a pipeline-compatible version

Tests whether the string begins with a certain substring. Equivalent to strings.HasPrefix

Example

input.tmpl:

{{if hasPrefix .Env.URL "https"}}foo{{else}}bar{{end}}
$ URL=http://example.com gomplate < input.tmpl
bar
$ URL=https://example.com gomplate < input.tmpl
foo

hasSuffix

See `strings.HasSuffix for a pipeline-compatible version

Tests whether the string ends with a certain substring. Equivalent to strings.HasSuffix

Example

input.tmpl:

{{.Env.URL}}{{if not (hasSuffix .Env.URL ":80")}}:80{{end}}
$ URL=http://example.com gomplate < input.tmpl
http://example.com:80

split

See `strings.Split for a pipeline-compatible version

Creates a slice by splitting a string on a given delimiter. Equivalent to strings.Split

Example

$ gomplate -i '{{range split "Bart,Lisa,Maggie" ","}}Hello, {{.}}{{end}}'
Hello, Bart
Hello, Lisa
Hello, Maggie

splitN

See `strings.SplitN for a pipeline-compatible version

Creates a slice by splitting a string on a given delimiter. The count determines the number of substrings to return. Equivalent to strings.SplitN

Example

$ gomplate -i '{{ range splitN "foo:bar:baz" ":" 2 }}{{.}}{{end}}'
foo
bar:baz

trim

See `strings.Trim for a pipeline-compatible version

Trims a string by removing the given characters from the beginning and end of the string. Equivalent to strings.Trim

Example

input.tmpl:

Hello, {{trim .Env.FOO " "}}!
$ FOO="  world " | gomplate < input.tmpl
Hello, world!