Skip to main content

Module string

Module string 

Source
Expand description

String case-conversion helpers.

All functions are pure and allocation-free when the input already matches the target convention.

Functionsยง

char_at
Get character at index.
contains
Check if string contains needle.
contains_all
Check if string contains all needles.
doesnt_contain
Check if string does NOT contain needle.
ends_with
Check if string ends with needle.
ensure_start
Ensure string starts with the given value.
escape_html
Escape HTML special characters.
finish
Ensure string ends with the given value.
invert_case
Invert the case of a string.
is_alphanumeric
Check if string contains only alphanumeric characters.
is_ascii
Check if string contains only ASCII characters.
is_empty
Check if string is empty (after trimming).
is_json
Check if string is a valid JSON string.
is_ulid
Check if string is a valid ULID.
is_url
Check if string is a valid URL.
is_uuid
Check if string is a valid UUID.
lcfirst
Convert first character of string to lowercase.
length
Return the character count (not byte count).
mask
Mask characters from index onward with the given character.
pad_both
Pad string to length on both sides.
pad_left
Pad string to length on the left.
pad_right
Pad string to length on the right.
password
Generate a secure password with letters, digits, and optionally symbols.
pluralize
Return a naive English plural of word.
position
Find position of needle in string.
pretty_duration
Convert nanoseconds to human-readable duration string.
random
Generate a cryptographically secure random string of given length. Uses URL-safe base64 encoding for the result.
repeat
Repeat a string times times.
replace_first
Replace first occurrence of from with to.
replace_last
Replace last occurrence of from with to.
reverse
Reverse a string.
slug
Convert a string to a URL-safe slug with the given separator.
squish
Collapse consecutive whitespace to single spaces and trim edges.
starts_with
Check if string starts with needle.
substr_count
Count occurrences of substring.
to_base64
Convert string to base64 encoding.
to_camel_case
Convert a string to camelCase.
to_dot_case
Convert a string to dot.case.
to_headline
Convert a string to Title Case (alias for to_title_case).
to_kebab_case
Convert a string to kebab-case.
to_lower
Convert a string to lowercase.
to_no_case
Convert a string to noCase (strips all casing).
to_pascal_case
Convert a string to PascalCase.
to_screaming_snake
Convert a string to SCREAMING_SNAKE_CASE.
to_sentence_case
Convert a string to Sentence case.
to_snake_case
Convert a string to snake_case.
to_title_case
Convert a string to Title Case.
to_upper
Convert a string to uppercase.
truncate
Truncate s to at most max characters, appending "..." if truncation occurs.
ucfirst
Convert first character of string to uppercase.
unwrap
Remove prefix and suffix if present.
word_count
Return the word count.
wrap
Wrap a string with prefix and suffix.