Strings

3 min read

The string operations expressions need most: case, trimming, prefixes and suffixes, splitting and joining, concatenation, case conversion between camelCase and kebab-case, and shortening text for display.

Reference

FunctionDescription
camelToKebabConverts CamelCase or PascalCase to kebab-case.
concatJoins any number of strings together with no separator.
containsReports whether substr is within s.
ellipsisShortens s to at most n characters, ending in “…” when it was cut.
hasPrefixReports whether s starts with prefix.
hasSuffixReports whether s ends with suffix.
joinJoins the elements of a list with sep between them.
repeatReturns s repeated count times.
replaceReturns s with every occurrence of old replaced by new.
splitSplits a string by a separator into a slice.
toLowerReturns s in lower case.
toUpperReturns s in upper case.
trimReturns s without the leading and trailing characters in cutset.
trimPrefixReturns s without prefix, if it starts with it.
trimSpaceReturns s without leading and trailing white space.
trimSuffixReturns s without suffix, if it ends with it.

Functions

camelToKebab

camelToKebab converts CamelCase or PascalCase to kebab-case. Useful when deriving Kubernetes resource names from Go type names.

{{ camelToKebab "WebsiteOperator" }}  → "website-operator"

concat

concat joins any number of strings together with no separator. Useful for building domain names, resource name prefixes, or any value assembled from multiple parts.

{{ concat "*." .spec.domain }}  → "*.api.example.com"

contains

contains reports whether substr is within s.

{{ contains .spec.image ":latest" }}  → true

ellipsis

ellipsis shortens s to at most n characters, ending in “…” when it was cut. For text people read (messages, descriptions); not for names or label values, which may not end in a dot: use truncate there.

{{ ellipsis .status.message 80 }}

hasPrefix

hasPrefix reports whether s starts with prefix.

{{ hasPrefix .spec.image "docker.io/" }}  → true

hasSuffix

hasSuffix reports whether s ends with suffix.

{{ hasSuffix .spec.host ".local" }}  → true

join

join joins the elements of a list with sep between them.

{{ join (podNames .pods) ", " }}  → "web-0, web-1, web-2"

repeat

repeat returns s repeated count times.

{{ repeat "-" 3 }}  → "---"

replace

replace returns s with every occurrence of old replaced by new.

{{ replace "payments-api" "-" "_" }}  → "payments_api"

split

split splits a string by a separator into a slice. Combine with index to extract a specific element.

{{ index (split "latest,stable,v2" ",") 0 }}  → "latest"

toLower

toLower returns s in lower case.

{{ toLower "Payments-API" }}  → "payments-api"

toUpper

toUpper returns s in upper case.

{{ toUpper .spec.env }}  → "PROD"

trim

trim returns s without the leading and trailing characters in cutset.

{{ trim .spec.path "/" }}  → "api"

trimPrefix

trimPrefix returns s without prefix, if it starts with it.

{{ trimPrefix .spec.image "docker.io/" }}

trimSpace

trimSpace returns s without leading and trailing white space.

{{ trimSpace .spec.name }}

trimSuffix

trimSuffix returns s without suffix, if it ends with it.

{{ trimSuffix .spec.host ".local" }}