Skip to content

string

String manipulation and formatting helpers.

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.7", default-features = false, features = ["string"] }

Opt-in sub-feature string-diacritics (adds the unicode-normalization dependency): remove_diacritics.

Import path: helpers4::string.

ItemWhat it does
camel_caseConverts s to camelCase.
capitalizeUppercases the first character of s and leaves the rest untouched.
constant_caseConverts s to CONSTANT_CASE (also known as SCREAMING_SNAKE_CASE).
dedentStrips the indentation shared by every non-blank line of s, and drops one leading and one trailing blank line.
escape_htmlEscapes the HTML special characters &, <, >, " and '.
escape_regexEscapes regular expression metacharacters (`.
excerptDerives a short, readable excerpt from a longer text — for a card, header or preview where a full paragraph does not fit.
indentPrefixes every non-blank line of s with prefix.
kebab_caseConverts s to kebab-case.
levenshtein_distanceThe Levenshtein edit distance between a and b: the fewest single-character insertions, deletions or substitutions needed to turn one into the other.
levenshtein_similarityNormalized levenshtein_distance between a and b, in [0, 1].
pascal_caseConverts s to PascalCase.
remove_diacriticsRemoves diacritical marks (accents) from s, e.g.
slugifyConverts s into a lowercase, hyphen-separated slug safe for URLs.
snake_caseConverts s to snake_case.
squishTrims s and collapses every run of whitespace into a single space.
title_caseCapitalizes the first letter of every whitespace-separated word and lowercases the rest.
truncateShortens s to at most max_chars characters, ending with suffix when it was cut.
unescape_htmlDecodes the HTML entities &amp;, &lt;, &gt;, &quot;, &apos;, &#39; and any numeric character reference (&#65;, &#x41;).
wordsSplits s into its words, keeping their original casing.