Skip to content

Stringable (Fluent String)

Larastvel's Stringable provides a fluent, chainable API for string manipulation. Wrap a string with Str::of() and chain methods together.

Basic Usage

rust
use larastvel_core::Str;

Str::of("hello_world")
    .studly()       // "HelloWorld"
    .upper();       // "HELLOWORLD"

Chaining

rust
Str::of("  Hello World  ")
    .trim()
    .slug("-")
    .upper();       // "HELLO-WORLD"

Str::of("the_quick_brown_fox")
    .replace("_", " ")
    .title()
    .replace(" ", "");  // "TheQuickBrownFox"

Case Conversion

rust
Str::of("hello_world").slug("-");       // "hello-world"
Str::of("hello_world").camel();         // "helloWorld"
Str::of("hello_world").studly();        // "HelloWorld"
Str::of("HelloWorld").snake();          // "hello_world"
Str::of("HelloWorld").kebab();          // "hello-world"

Str::of("hello").upper();               // "HELLO"
Str::of("HELLO").lower();               // "hello"
Str::of("hello").ucfirst();             // "Hello"
Str::of("Hello").lcfirst();             // "hello"

Str::of("the quick brown fox").title(); // "The Quick Brown Fox"
Str::of("hello_world").headline();      // "Hello World"

Substring Operations

rust
Str::of("Hello World").after("Hello ");       // "World"
Str::of("Hello World").before(" World");      // "Hello"
Str::of("[Hello]").between("[", "]");         // "Hello"
Str::of("Hello World").substr(0, Some(5));    // "Hello"
Str::of("Hello World").limit(5, "...");       // "Hello..."

Query Methods

rust
let s = Str::of("Hello World");
s.contains("World");          // true
s.contains_all(&["Hello", "World"]); // true
s.starts_with("Hello");       // true
s.ends_with("World");         // true
s.length();                   // 11
s.is_ascii();                 // true
s.is_json();                  // false
s.is_url();                   // false
s.is_uuid();                  // false
s.word_count();               // 2
s.position("World", 0);       // Some(6)

Padding & Masking

rust
Str::of("Hello").pad_left(7, "*");     // "**Hello"
Str::of("Hello").pad_right(7, "*");    // "Hello**"
Str::of("Hello").pad_both(9, "*");     // "**Hello**"
Str::of("1234-5678").mask("*", 0, 4);  // "****-5678"

Transformation

rust
Str::of("Hello World").replace("World", "Moon");   // "Hello Moon"
Str::of("foo bar foo").replace_first("foo", "baz"); // "baz bar foo"
Str::of("ab").repeat(3);                             // "ababab"
Str::of("  hello  ").trim();                         // "hello"
Str::of("/hello/").trim_slashes();                   // "hello"
Str::of("hello").finish("/");                        // "hello/"
Str::of("world").start("hello ");                    // "hello world"
Str::of("Hello").append(" World");                   // "Hello World"
Str::of("World").prepend("Hello ");                  // "Hello World"

Conversion

rust
let s = Str::of("hello");
s.to_string();               // "hello" (Display trait)
let s: String = s.into();    // Into<String>
s.value();                   // &str
s.into_value();              // String (consuming)

Available Methods

MethodReturnsDescription
slug(sep)SelfURL-friendly slug
camel()SelfConvert to camelCase
studly()SelfConvert to StudlyCase
snake()SelfConvert to snake_case
kebab()SelfConvert to kebab-case
title()SelfConvert to Title Case
headline()SelfConvert to Headline Case
ucfirst()SelfUppercase first character
lcfirst()SelfLowercase first character
upper()SelfConvert to uppercase
lower()SelfConvert to lowercase
trim()SelfTrim whitespace
trim_slashes()SelfTrim slashes
replace(search, replace)SelfReplace all occurrences
replace_first(search, replace)SelfReplace first occurrence
repeat(times)SelfRepeat string
after(search)SelfEverything after substring
before(search)SelfEverything before substring
between(from, to)SelfString between two substrings
limit(len, end)SelfTruncate to length
substr(start, len)SelfSubstring by character positions
pad_left(len, pad)SelfLeft-pad
pad_right(len, pad)SelfRight-pad
pad_both(len, pad)SelfPad both sides
mask(char, start, len)SelfMask portion
finish(cap)SelfEnsure string ends with character
start(prefix)SelfEnsure string starts with prefix
append(suffix)SelfAppend to string
prepend(prefix)SelfPrepend to string
contains(needle)boolCheck substring
contains_all(needles)boolCheck all substrings
starts_with(needle)boolCheck prefix
ends_with(needle)boolCheck suffix
position(needle, offset)Option<usize>Find position
length()usizeCharacter count
is_ascii()boolCheck if all ASCII
is_json()boolCheck if valid JSON
is_url()boolCheck if http(s)😕/
is_uuid()boolCheck if valid UUID
word_count()usizeCount words

Released under the MIT License.