Strings

The Strings registry offers a comprehensive set of functions for manipulating strings, including formatting, splitting, joining, and other common string operations.

You can easily import all the functions from the strings registry by including the following import statement in your code

import "github.com/go-sprout/sprout/registry/strings"

nospace

The function removes all whitespace characters from the provided string, eliminating any spaces, tabs, or line breaks.

Signature

Nospace(str string) string

Must version

{{ "Hello World" | nospace }} // Output: "HelloWorld"

trim

The function removes any leading and trailing whitespace from the provided string.

Signature

Trim(str string) string

Must version

{{ " Hello World " | trim }} // Output: "Hello World"

trimAll

The function removes all instances of any characters in the 'cutset' from both the beginning and the end of the provided string.

Signature

TrimAll(cutset string, str string)

Must version

{{ "xyzHelloxyz" | trimAll "xyz" }} // Output: "Hello"

trimPrefix

The function removes the specified 'prefix' from the start of the provided string if it is present.

Signature

TrimPrefix(prefix string, str string) string

Must version

{{ "HelloWorld" | trimPrefix "Hello" }} // Output: "World"
{{ "HelloWorld" | trimPrefix "World" }} // Output: "HelloWorld"

trimSuffix

The function removes the specified 'suffix' from the end of the provided string if it is present.

Signature

TrimSuffix(suffix string, str string) string

Must version

{{ "HelloWorld" | trimSuffix "Hello" }} // Output: "HelloWorld"
{{ "HelloWorld" | trimSuffix "World" }} // Output: "Hello"

contains

The function checks whether the provided string contains the specified substring.

Signature

Contains(substring string, str string) bool

Must version

{{ "Hello" | contains "ell" }} // Output: true

hasPrefix

The function checks whether the provided string starts with the specified prefix.

Signature

HasPrefix(prefix string, str string) bool

Must version

{{ "HelloWorld" | hasPrefix "Hello" }} // Output: true

hasSuffix

The function checks whether the provided string ends with the specified suffix.

Signature

HasSuffix(suffix string, str string) bool

Must version

{{ "HelloWorld" | hasSuffix "World" }} // Output: true

toLower

The function converts all characters in the provided string to lowercase.

Signature

ToLower(str string) string

Must version

{{ "HELLO WORLD" | toLower }} // Output: "hello world"

toUpper

The function converts all characters in the provided string to uppercase.

Signature

ToUpper(str string) string

Must version

{{ "hello world" | toUpper }} // Output: "HELLO WORLD"

replace

The function replaces all occurrences of a specified substring ('old') in the source string with a new substring ('new').

Signature

Replace(old string, new string, src string) string

Must version

{{ "banana" | replace "a", "o" }} // Output: "bonono"

repeat

The function repeats the provided string a specified number of times.

Signature

Repeat(count int, str string) string

Must version

{{ "ha" | repeat 3 }} // Output: "hahaha"

join

The function concatenates elements of a slice into a single string, with each element separated by a specified delimiter. It can convert various slice types to a slice of strings if needed before joining.

Signature

Join(sep string, v any) string

Must version

{{ $list := slice "apple" "banana" "cherry" }}
{{ $list | join ", " }} // Output: "apple, banana, cherry"

trunc

The function truncates the provided string to a maximum specified length. If the length is negative, it removes the specified number of characters from the beginning of the string.

Signature

Trunc(count int, str string) string

Must version

{{ "Hello World" | trunc 5 }} // Output: "Hello"
{{ "Hello World" | trunc -5 }} // Output: "World"

shuffle

The function randomly rearranges the characters in the provided string, producing a shuffled version of the original string.

Signature

Shuffle(str string) string

Must version

{{ "hello" | shuffle }} // Output: "loleh" (output may vary due to randomness)

ellipsis

The function truncates a string to a specified maximum width and appends an ellipsis ("...") if the string exceeds that width.

Signature

Ellipsis(maxWidth int, str string) string

Must version

{{ "Hello World" | ellipsis 10 }} // Output: "Hello W..."

ellipsisBoth

The function truncates a string from both ends, preserving the middle portion and adding ellipses ("...") to both ends if the string exceeds the specified length.

Signature

EllipsisBoth(offset int, maxWidth int, str string) string

Must version

{{ "Hello World" | ellipsisBoth 1 10 }} // Output: "...lo Wor..."

initials

The function extracts initials from a string, optionally using specified delimiters to identify word boundaries.

Signature

Initials(str string) string

Must version

{{ "John Doe" | initials }} // Output: "JD"

plural

The function returns a specified string ('one') if the count is 1; otherwise, it returns an alternative string ('many').

Signature

Plural(one, many string, count int) string

Must version

{{ 1 | plural "apple" "apples" }} // Output: "apple"
{{ 2 | plural "apple" "apples" }} // Output: "apples"

wrap

The function breaks a string into lines, ensuring that each line does not exceed a specified maximum length. It avoids splitting words across lines unless absolutely necessary.

Signature

Wrap(length int, str string) string

Must version

{{ "This is a long string that needs to be wrapped." | wrap 10 }}
// Output: "This is a\nlong\nstring\nthat needs\nto be\nwrapped."

wrapWith

The function breaks a string into lines with a specified maximum length, using a custom newline character to separate the lines. It only wraps words when they exceed the maximum line length.

Signature

WrapWith(length int, newLineCharacter string, str string) string

Must version

{{ "This is a long string that needs to be wrapped." | wrapWith 10 "<br>" }}
// Output: "This is a<br>long<br>string<br>that needs<br>to be<br>wrapped."

quote

The function wraps each element in a provided list with double quotes and separates them with spaces.

Signature

Quote(elements ...any) string

Must version

{{ $list := slice "hello" "world" 123 }}
{{ $list | quote }}
// Output: "hello" "world" "123"

squote

The function wraps each element in the provided list with single quotes and separates them with spaces.

Signature

Squote(elements ...any) string

Must version

{{ $list := slice "hello" "world" 123 }}
{{ $list | squote }}
// Output: 'hello' 'world' '123'

toCamelCase

Converts a string to camelCase format.

Signature

ToCamelCase(str string) string

Must version

{{ "hello world" | toCamelCase }} // Output: "helloWorld"

toKebabCase

Converts a string to kebab-case format.

Signature

ToKebabCase(str string) string

Must version

{{ "hello world" | toKebabCase }} // Output: "hello-world"

toPascalCase

Converts a string to PascalCase format.

Signature

ToPascalCase(str string) string

Must version

{{ "hello world" | toPascalCase }} // Output: "HelloWorld"

toDotCase

Converts a string to dot.case format.

Signature

ToDotCase(str string) string

Must version

{{ "hello world" | toDotCase }} // Output: "hello.world"

toPathCase

Converts a string to path/case format.

Signature

ToPathCase(str string) string

Must version

{{ "hello world" | toPathCase }} // Output: "hello/world"

toConstantCase

Converts a string to CONSTANT_CASE format.

Signature

ToConstantCase(str string) string

Must version

{{ "hello world" | toConstantCase }} // Output: "HELLO_WORLD"

toSnakeCase

Converts a string to snake_case format.

Signature

ToSnakeCase(str string) string

Must version

{{ "hello world" | toSnakeCase }} // Output: "hello_world"

toTitleCase

Converts a string to Title Case format.

Signature

ToTitleCase(str string) string

Must version

{{ "hello world" | toTitleCase }} // Output: "Hello World"

untitle

Converts the first letter of each word in a string to lowercase.

Signature

Untitle(str string) string

Must version

{{ "Hello World" | untitle }} // Output: "hello world"

swapCase

Switches the case of each letter in a string, converting lowercase to uppercase and vice versa.

Signature

SwapCase(str string) string

Must version

{{ "Hello World" | swapCase }} // Output: "hELLO wORLD"

split

Divides a string into a map of parts based on a specified separator, returning a collection of the split components.

Signature

Split(sep, orig string) map[string]string

Must version

{{ "apple,banana,cherry" | split "," }}
// Output: { "_0":"apple", "_1":"banana", "_2":"cherry" }

splitn

Splits a string into a specified number of parts using a separator, returning a map with up to n elements.

Signature

Splitn(sep string, n int, orig string) map[string]string

Must version

{{ "apple,banana,cherry" | split "," 2 }}
// Output: { "_0":"apple", "_1":"banana,cherry" }

substr

Extracts a portion of a string based on given start and end positions, with support for negative indices to count from the end.

Signature

Substring(start int, end int, str string) string

Must version

{{ "Hello World" | substr 0 5 }} // Output: "Hello"

indent

Adds spaces to the beginning of each line in a string, effectively indenting the text.

Signature

Indent(spaces int, str string) string

Must version

{{ "Hello\nWorld" | indent 4 }} // Output: "    Hello\n    World"

nindent

Similar to Indent, but also adds a newline before the indented lines.

Signature

Nindent(spaces int, str string) string

Must version

{{ "Hello\nWorld" | nindent 4 }} // Output: "\n    Hello\n    World"

seq

Generates a sequence of numbers as a string, allowing for customizable start, end, and step values, similar to the Unix seq command.

Signature

Seq(params ...int) string

Must version

{{ seq 1, 2, 10 }} // Output: "1 3 5 7 9"
{{ seq 3 -3 2 }} // Output: "3"
{{ seq }} // Output: ""
{{ seq 0 4 }} // Output: "0 1 2 3 4"
{{ seq -5 }} // Output: "1 0 -1 -2 -3 -4 -5"
{{ seq 0 -4 }} // Output: "0 -1 -2 -3 -4"

Last updated

#18: v0.5 loaders with handlers and registries

Change request updated