Skip to content

Strings

David Ciulla edited this page Nov 7, 2026 · 2 revisions

Strings

The Laravex.Support.Str module provides a collection of string manipulation helpers modelled after Laravel's Str facade. Access it directly or via the str() Helpers.

alias Laravex.Support.Str

# Or via the helper
import Laravex.Support.Helpers
str().slug("Hello World")

Case Conversion

Str.lower("Hello World")
# => "hello world"

Str.upper("Hello World")
# => "HELLO WORLD"

Str.title("hello world")
# => "Hello World"

Str.camel("hello_world")
# => "HelloWorld"

Str.studly("hello_world")
# => "HelloWorld"   (alias for camel/1)

Str.snake("HelloWorld")
# => "hello_world"

Str.kebab("hello_world")
# => "hello-world"

camel/1 and studly/1 delegate to Macro.camelize/1. snake/1 delegates to Macro.underscore/1. kebab/1 applies snake/1 then replaces underscores with hyphens.


Searching

Str.contains?("Hello World", "World")
# => true

# Check for any of several needles
Str.contains?("Hello World", ["foo", "World"])
# => true

Str.starts_with?("Hello World", "Hello")
# => true

Str.ends_with?("Hello World", "World")
# => true

Trimming and Limiting

Str.limit("The quick brown fox", 10)
# => "The quick ..."

Str.limit("The quick brown fox", 10, " →")
# => "The quick  →"

Str.squish("  Hello   World  ")
# => "Hello World"

limit/3 truncates to max characters and appends ending (default "..."). If the string is already within the limit, it is returned unchanged.

squish/1 collapses all runs of whitespace (including leading/trailing) into single spaces.


Extracting Substrings

Str.before("user@example.com", "@")
# => "user"

Str.after_value("user@example.com", "@")
# => "example.com"

Appending and Ensuring Endings

Str.finish("https://example.com", "/")
# => "https://example.com/"

Str.finish("https://example.com/", "/")
# => "https://example.com/"   (already ends with "/")

Slugs

Str.slug("Hello World!")
# => "hello-world"

Str.slug("Hello World!", "_")
# => "hello_world"

slug/2 lowercases the string, replaces all non-alphanumeric runs with the separator, then trims the separator from both ends.


Replacement and Masking

Str.replace("Hello World", "World", "Elixir")
# => "Hello Elixir"

# Mask characters starting at index 0
Str.mask("secret", "*")
# => "******"

# Mask starting at a specific index
Str.mask("1234567890", "*", 4)
# => "1234******"

# Mask a specific number of characters
Str.mask("hello@example.com", "*", 5, 9)
# => "hello*********com"

mask/4 replaces length characters starting at index with the given character. Index is zero-based. Both index and length are clamped to the string boundaries.


Length

Str.length("Hello")
# => 5

Uses String.length/1, so it counts Unicode graphemes correctly.


Random Strings

Str.random()     # 16-character URL-safe random string
Str.random(32)   # 32-character URL-safe random string

Generated via :crypto.strong_rand_bytes/1 encoded with Base.url_encode64/2 (no padding). The result is always URL-safe.


UUID Generation

Str.uuid()
# => "550e8400-e29b-41d4-a716-446655440000"

Generates a version 4 UUID from :crypto.strong_rand_bytes(16). The variant and version bits are set according to RFC 4122.


ULID Generation

Str.ulid()
# => "01ARZ3NDEKTSV4RRFFQ69G5FAV"

Generates a Crockford Base32 ULID (Universally Unique Lexicographically Sortable Identifier). Time-sortable and URL-safe.


Password Generation

# 32-character password with letters, numbers, and symbols (default)
Str.password()
Str.password(16)

# Customise character classes
Str.password(24, numbers: true, symbols: false)

Excerpt

Extract a snippet of text around a search phrase (Laravel Str::excerpt):

Str.excerpt("Buy Adobe Acrobat Reader DC", "Acrobat", radius: 5)
# => "...Adobe Acrobat Reader..."

Str.excerpt(text, "phrase", radius: 100, omission: "…")

radius controls how many characters are shown on each side of the phrase. When phrase is "", the string is simply truncated to radius characters.


Inspection Helpers

Str.is_uuid?("550e8400-e29b-41d4-a716-446655440000")  # => true
Str.is_ulid?("01ARZ3NDEKTSV4RRFFQ69G5FAV")            # => true
Str.is_url?("https://example.com")                     # => true
Str.is_json?("{\"name\":\"Alice\"}")                   # => true
Str.is_ascii?("hello")                                 # => true
Str.is_ip?("192.168.1.1")                              # => true
Str.is_ipv4?("10.0.0.1")                               # => true
Str.is_ipv6?("::1")                                    # => true
Str.is_mac_address?("AA:BB:CC:DD:EE:FF")               # => true

See Also

Clone this wiki locally