str
Unicode-aware string utilities for Gleam.
This module re-exports the most commonly used functions from str/core
and str/extra for convenient access. For the full API, import the
submodules directly.
Quick Start
import str
str.truncate("Hello π¨βπ©βπ§βπ¦ World", 10, "...") // "Hello π¨βπ©βπ§βπ¦..."
str.slugify("Crème Brûlée") // "creme-brulee"
str.similarity("hello", "hallo") // 0.8
Types
pub type FillPosition =
core.FillPosition
Values
pub fn at(text: String, index: Int) -> Result(String, Nil)
Returns the grapheme cluster at the given index (0-based).
pub fn capitalize(text: String) -> String
Capitalizes text: first letter uppercase, rest lowercase.
pub fn center(text: String, width: Int, pad: String) -> String
Centers text within the specified width.
pub fn contains(text: String, needle: String) -> Bool
Returns True if needle is found in text (grapheme-aware).
pub fn contains_all(text: String, needles: List(String)) -> Bool
Returns True if all of the needles appear in text.
pub fn contains_any(text: String, needles: List(String)) -> Bool
Returns True if any of the needles appear in text.
pub fn distance(a: String, b: String) -> Int
Calculates Levenshtein distance between two strings.
pub fn ellipsis(text: String, max_len: Int) -> String
Truncates text with ellipsis (β¦).
pub fn ends_with(text: String, suffix: String) -> Bool
Returns True if text ends with suffix on grapheme boundaries.
pub fn index_of(text: String, needle: String) -> Result(Int, Nil)
Finds the index of the first occurrence of needle (grapheme-aware).
pub fn is_lowercase(text: String) -> Bool
Checks if all cased characters are lowercase.
pub fn is_title_case(text: String) -> Bool
Checks if text is in Title Case format.
pub fn is_uppercase(text: String) -> Bool
Checks if all cased characters are uppercase.
pub fn last_index_of(
text: String,
needle: String,
) -> Result(Int, Nil)
Finds the index of the last occurrence of needle.
pub fn normalize_whitespace(text: String) -> String
Normalizes whitespace: collapses to single spaces and trims.
pub fn pad_left(text: String, width: Int, pad: String) -> String
Pads text on the left to reach the specified width.
pub fn pad_right(text: String, width: Int, pad: String) -> String
Pads text on the right to reach the specified width.
pub fn replace_first(
text: String,
old: String,
new: String,
) -> String
Replaces only the first occurrence of old with new.
pub fn replace_last(
text: String,
old: String,
new: String,
) -> String
Replaces only the last occurrence of old with new.
pub fn similarity(a: String, b: String) -> Float
Calculates similarity as a percentage (0.0 to 1.0).
pub fn starts_with(text: String, prefix: String) -> Bool
Returns True if text starts with prefix on grapheme boundaries.
pub fn take(text: String, n: Int) -> String
Returns the first N grapheme clusters from text.
pub fn truncate(
text: String,
max_len: Int,
suffix: String,
) -> String
Truncates text to max_len graphemes, preserving emoji sequences.