Str holds the string helpers worth carrying over from Laravel: slugs,
masks, limits, excerpts, and plural and singular forms. For case
conversions, use the heck crate; for everything else, std::str and
regex. Every count here is in characters, never bytes, so a multibyte
value is never cut inside a character.
use Str;
let slug = slug; // oeuvre-dart-at-home
let card = mask; // ***************1234
let teaser = limit; // The quick brown...
let label = plural; // comments
Slugs
Str::slug(title, separator) spells the title in ASCII, lower-cases it,
writes @ as at, and joins each run of other characters between
letters and digits with the separator:
use Str;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The ASCII spelling is Laravel's own: the replacement map of
voku/portable-ascii, the package Laravel's Str::ascii uses, so a title
gets the slug it gets in Laravel. That map spells Latin, Greek, Cyrillic,
Arabic, and many other scripts, and drops what it can't spell, such as
Han characters and emoji: Str::slug("北京 city", "-") is city.
Masks
Str::mask(value, character, index, length) replaces the characters from
index with character: length of them, or all to the end with None.
A negative index counts from the end, and a negative length stops that
many short of the end:
use Str;
assert_eq!;
assert_eq!;
Limits and excerpts
Str::limit(value, limit, end) keeps the first limit characters and
adds end when it cut. Str::limit_words cuts at the last space within
the limit instead, so no word is broken:
use Str;
assert_eq!;
assert_eq!;
Str::excerpt(text, phrase, radius, omission) frames the first match of
a phrase, ignoring case, with up to radius characters on each side,
in text of any number of lines. It returns None when the phrase is not
there:
use Str;
assert_eq!;
Plural and singular
Str::plural(word, count) and Str::singular(word) inflect by the rules
of the current locale's language, the ones Laravel's Pluralizer uses
from doctrine/inflector 2.1.0, the release Laravel 13 installs: English,
French, Norwegian Bokmål, Portuguese, Spanish, and Turkish. Any other
language uses the English rules. A count of 1 or -1 leaves the word as it
is, and the result keeps the word's case:
use Str;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
In a request whose locale is fr, Str::plural("cheval", 2) is
chevaux; in es, Str::plural("ciudad", 2) is ciudades. Without the
localization feature, the rules are English.
Both read the current locale, so a page that RenderCache
stores and that calls them must vary by it: add
.vary(VarianceDimension::Locale) to the route's policy. Without it,
RenderCache declines to store the page.
To choose the form a reader sees in a translated message, use the plural
categories of a Fluent message instead; see
Localization. Str::plural is for words in your own
code, such as a table or a label built from a model's name.
Why Suprnova diverges
- The language follows the locale. Laravel sets the plural language
for the whole process with
Pluralizer::useLanguage; here the current request's locale chooses it, forStr::singularas forStr::plural. - An excerpt can span lines. Laravel's pattern stops at a line break,
so
Str::excerptreturnsnullfor text with a line break inside it; here it finds the phrase on any line. - Counts are characters. Laravel's
limitcounts display width, two for a wide East Asian character, and its word-preserving form also strips HTML tags. - A subset. There is no fluent
Stringable; case conversions come fromheck.
Next
- Localization - percentages, abbreviated numbers, and the rest of the locale-aware formatting
- Validation - rules for the strings your users send
