Strings

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

circle-info

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

Must version

❌

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

trim

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

Signature

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

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

Must version

❌

trimSuffix

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

Signature

Must version

❌

contains

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

Signature

Must version

❌

hasPrefix

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

Signature

Must version

❌

hasSuffix

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

Signature

Must version

❌

toLower

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

Signature

Must version

❌

toUpper

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

Signature

Must version

❌

replace

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

Signature

Must version

❌

repeat

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

Signature

Must version

❌

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

Must version

❌

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

Must version

❌

shuffle

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

Signature

Must version

❌

ellipsis

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

Signature

Must version

❌

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

Must version

❌

initials

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

Signature

Must version

❌

plural

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

Signature

Must version

❌

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

Must version

❌

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

Must version

❌

quote

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

Signature

Must version

❌

squote

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

Signature

Must version

❌

toCamelCase

Converts a string to camelCase format.

Signature

Must version

❌

toKebabCase

Converts a string to kebab-case format.

Signature

Must version

❌

toPascalCase

Converts a string to PascalCase format.

Signature

Must version

❌

toDotCase

Converts a string to dot.case format.

Signature

Must version

❌

toPathCase

Converts a string to path/case format.

Signature

Must version

❌

toConstantCase

Converts a string to CONSTANT_CASE format.

Signature

Must version

❌

toSnakeCase

Converts a string to snake_case format.

Signature

Must version

❌

toTitleCase

Converts a string to Title Case format.

Signature

Must version

❌

untitle

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

Signature

Must version

❌

swapCase

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

Signature

Must version

❌

split

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

Signature

Must version

❌

splitn

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

Signature

Must version

❌

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

Must version

❌

indent

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

Signature

Must version

❌

nindent

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

Signature

Must version

❌

seq

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

Signature

Must version

❌

Last updated