pub trait Str: Sealed {
Show 44 methods
// Required methods
fn snake(&self) -> String;
fn kebab(&self) -> String;
fn studly(&self) -> String;
fn camel(&self) -> String;
fn slug(&self) -> String;
fn slug_with(&self, separator: &str) -> String;
fn title(&self) -> String;
fn headline(&self) -> String;
fn upper_first(&self) -> String;
fn lower_first(&self) -> String;
fn split_upper(&self) -> Vec<&str>;
fn plural(&self) -> String;
fn singular(&self) -> String;
fn ascii(&self) -> String;
fn after(&self, search: &str) -> &str;
fn after_last(&self, search: &str) -> &str;
fn before(&self, search: &str) -> &str;
fn before_last(&self, search: &str) -> &str;
fn between(&self, from: &str, to: &str) -> &str;
fn between_first(&self, from: &str, to: &str) -> &str;
fn chop_start(&self, prefix: &str) -> &str;
fn chop_end(&self, suffix: &str) -> &str;
fn take(&self, count: usize) -> &str;
fn start(&self, prefix: &str) -> String;
fn finish(&self, cap: &str) -> String;
fn replace_last(&self, search: &str, replace: &str) -> String;
fn replace_start(&self, search: &str, replace: &str) -> String;
fn replace_end(&self, search: &str, replace: &str) -> String;
fn swap(&self, replacements: &[(&str, &str)]) -> String;
fn resembles(&self, pattern: &str) -> bool;
fn limit(&self, limit: usize) -> String;
fn limit_with(&self, limit: usize, end: &str) -> String;
fn words(&self, words: usize) -> String;
fn words_with(&self, words: usize, end: &str) -> String;
fn word_count(&self) -> usize;
fn excerpt(&self, phrase: &str, radius: usize) -> Option<String>;
fn word_wrap(&self, width: usize) -> String;
fn squish(&self) -> String;
fn deduplicate(&self, character: char) -> String;
fn mask(&self, character: char, range: impl RangeBounds<usize>) -> String;
fn pad_start(&self, width: usize, pad: char) -> String;
fn pad_end(&self, width: usize, pad: char) -> String;
fn pad_both(&self, width: usize, pad: char) -> String;
fn digits(&self) -> String;
}Expand description
String helpers for every str, such as case conversions, slugs, substrings, shortening,
padding, and plurals.
use laravel::str::Str;
let name = String::from("Blog Post Title");
assert_eq!(name.snake(), "blog_post_title");
assert_eq!(name.camel(), "blogPostTitle");
assert_eq!("app/Http/Kernel.rs".after_last("/"), "Kernel.rs");Lengths, positions, and widths count characters, not bytes.
Required Methods§
Sourcefn snake(&self) -> String
fn snake(&self) -> String
Converts the string to snake case, splitting words before each capital letter and at whitespace.
BlogPostTitle and Blog Post Title both become blog_post_title. A string of lowercase
letters stays as it is.
Sourcefn kebab(&self) -> String
fn kebab(&self) -> String
Converts the string to kebab case, so BlogPostTitle becomes blog-post-title.
Sourcefn studly(&self) -> String
fn studly(&self) -> String
Converts the string to studly case, also called Pascal case, splitting words at whitespace,
-, and _. blog_post_title becomes BlogPostTitle.
Sourcefn camel(&self) -> String
fn camel(&self) -> String
Converts the string to camel case, so blog_post_title becomes blogPostTitle.
Sourcefn slug(&self) -> String
fn slug(&self) -> String
Returns a URL-friendly slug with words joined by -.
Hello World! becomes hello-world, and user@host becomes user-at-host. The slug keeps
letters and digits as they are, accents included, and drops other characters.
Sourcefn slug_with(&self, separator: &str) -> String
fn slug_with(&self, separator: &str) -> String
Returns a slug with words joined by separator, so "Hello World".slug_with("_") returns
hello_world.
Sourcefn title(&self) -> String
fn title(&self) -> String
Converts the string to title case, with each word’s first letter in uppercase and the rest
in lowercase, so a nice title becomes A Nice Title.
A word starts after any character that isn’t a letter, a digit, or an apostrophe, so
o'neil's first-class becomes O'neil's First-Class.
Sourcefn headline(&self) -> String
fn headline(&self) -> String
Splits the string into words at whitespace, -, _, and capitals, then joins them with
spaces in title case.
email_notification_sent and EmailNotificationSent both become Email Notification Sent.
Sourcefn upper_first(&self) -> String
fn upper_first(&self) -> String
Converts the first letter to uppercase, so taylor otwell becomes Taylor otwell.
Sourcefn lower_first(&self) -> String
fn lower_first(&self) -> String
Converts the first letter to lowercase, so Taylor becomes taylor.
Sourcefn split_upper(&self) -> Vec<&str>
fn split_upper(&self) -> Vec<&str>
Splits the string before each uppercase letter, so laravelFramework becomes
["laravel", "Framework"].
Sourcefn plural(&self) -> String
fn plural(&self) -> String
Returns the plural of an English word, or of the last word in a name, keeping its case.
post becomes posts, child becomes children, and BlogPost becomes BlogPosts. A
word that’s already plural stays as it is. So do uncountable words, such as sheep, and
words that end in a letter outside ASCII, such as café.
Sourcefn singular(&self) -> String
fn singular(&self) -> String
Returns the singular of an English word, or of the last word in a name, following the same
rules as plural. categories becomes category.
Sourcefn after(&self, search: &str) -> &str
fn after(&self, search: &str) -> &str
Returns what follows the first search, or the whole string when it doesn’t contain
search. "app/Http/Kernel".after("/") returns Http/Kernel.
Sourcefn after_last(&self, search: &str) -> &str
fn after_last(&self, search: &str) -> &str
Returns what follows the last search, or the whole string when it doesn’t contain
search. "app/Http/Kernel".after_last("/") returns Kernel.
Sourcefn before(&self, search: &str) -> &str
fn before(&self, search: &str) -> &str
Returns what comes before the first search, or the whole string when it doesn’t contain
search. "app/Http/Kernel".before("/") returns app.
Sourcefn before_last(&self, search: &str) -> &str
fn before_last(&self, search: &str) -> &str
Returns what comes before the last search, or the whole string when it doesn’t contain
search. "app/Http/Kernel".before_last("/") returns app/Http.
Sourcefn between(&self, from: &str, to: &str) -> &str
fn between(&self, from: &str, to: &str) -> &str
Returns what’s between the first from and the last to after it, so
"[a] and [b]".between("[", "]") returns a] and [b.
It returns the whole string when from or to is empty.
Sourcefn between_first(&self, from: &str, to: &str) -> &str
fn between_first(&self, from: &str, to: &str) -> &str
Returns what’s between the first from and the first to after it, so
"[a] and [b]".between_first("[", "]") returns a.
Sourcefn chop_start(&self, prefix: &str) -> &str
fn chop_start(&self, prefix: &str) -> &str
Removes one prefix from the start of the string, if it’s there, so
"https://laravel.com".chop_start("https://") returns laravel.com.
Sourcefn chop_end(&self, suffix: &str) -> &str
fn chop_end(&self, suffix: &str) -> &str
Removes one suffix from the end of the string, if it’s there, so
"app/Models/Photograph.rs".chop_end(".rs") returns app/Models/Photograph.
Sourcefn take(&self, count: usize) -> &str
fn take(&self, count: usize) -> &str
Returns the first count characters, so "Build something amazing!".take(5) returns
Build.
Sourcefn start(&self, prefix: &str) -> String
fn start(&self, prefix: &str) -> String
Makes the string start with a single prefix. "this/string".start("/") and
"//this/string".start("/") both return /this/string.
Sourcefn finish(&self, cap: &str) -> String
fn finish(&self, cap: &str) -> String
Makes the string end with a single cap. "this/string".finish("/") and
"this/string//".finish("/") both return this/string/.
Sourcefn replace_last(&self, search: &str, replace: &str) -> String
fn replace_last(&self, search: &str, replace: &str) -> String
Replaces the last search, so "the fox the dog".replace_last("the", "a") returns
the fox a dog.
Sourcefn replace_start(&self, search: &str, replace: &str) -> String
fn replace_start(&self, search: &str, replace: &str) -> String
Replaces search only when the string starts with it, so
"Hello World".replace_start("Hello", "Laravel") returns Laravel World.
Sourcefn replace_end(&self, search: &str, replace: &str) -> String
fn replace_end(&self, search: &str, replace: &str) -> String
Replaces search only when the string ends with it, so
"Hello World".replace_end("World", "Laravel") returns Hello Laravel.
Sourcefn swap(&self, replacements: &[(&str, &str)]) -> String
fn swap(&self, replacements: &[(&str, &str)]) -> String
Replaces each key with its value in a single pass, trying longer keys first, so a replacement is never replaced again.
"Tacos are great!".swap(&[("Tacos", "Burritos"), ("great", "fantastic")]) returns
Burritos are fantastic!.
Sourcefn resembles(&self, pattern: &str) -> bool
fn resembles(&self, pattern: &str) -> bool
Returns whether the string matches a pattern in which * stands for any characters,
including none. "posts/1/edit".resembles("posts/*") returns true.
Matching is case-sensitive.
Sourcefn limit(&self, limit: usize) -> String
fn limit(&self, limit: usize) -> String
Cuts the string to limit characters and ends it with ... when it was longer, so
"The quick brown fox".limit(9) returns The quick....
The ... doesn’t count toward the limit, and limit trims whitespace before it.
Sourcefn limit_with(&self, limit: usize, end: &str) -> String
fn limit_with(&self, limit: usize, end: &str) -> String
Cuts the string to limit characters and ends it with end when it was longer.
Sourcefn words(&self, words: usize) -> String
fn words(&self, words: usize) -> String
Cuts the string to words words and ends it with ... when there were more, so
"Perfectly balanced, as all things should be.".words(3) returns
Perfectly balanced, as....
Sourcefn words_with(&self, words: usize, end: &str) -> String
fn words_with(&self, words: usize, end: &str) -> String
Cuts the string to words words and ends it with end when there were more.
Sourcefn word_count(&self) -> usize
fn word_count(&self) -> usize
Returns the number of words, counting words separated by whitespace.
Sourcefn excerpt(&self, phrase: &str, radius: usize) -> Option<String>
fn excerpt(&self, phrase: &str, radius: usize) -> Option<String>
Returns the text around the first match of phrase, ignoring case, with up to radius
characters on each side and ... where the text is cut.
"This is my name".excerpt("my", 3) returns ...is my na.... It returns None when the
string doesn’t contain phrase.
Sourcefn word_wrap(&self, width: usize) -> String
fn word_wrap(&self, width: usize) -> String
Breaks the string into lines at spaces, so each line is at most width characters where
its words allow. A word longer than width gets a line of its own.
Sourcefn squish(&self) -> String
fn squish(&self) -> String
Trims the string and replaces each run of whitespace with a single space, so
" laravel framework " becomes laravel framework.
Sourcefn deduplicate(&self, character: char) -> String
fn deduplicate(&self, character: char) -> String
Replaces each run of character with a single character, so
"The Laravel Framework".deduplicate(' ') returns The Laravel Framework.
Sourcefn mask(&self, character: char, range: impl RangeBounds<usize>) -> String
fn mask(&self, character: char, range: impl RangeBounds<usize>) -> String
Replaces the characters in range with character, so
"taylor@example.com".mask('*', 3..) returns tay***************.
The range counts characters, and mask ignores positions past the end.
Sourcefn pad_start(&self, width: usize, pad: char) -> String
fn pad_start(&self, width: usize, pad: char) -> String
Pads the start of the string with pad to width characters, so
"42".pad_start(5, '0') returns 00042.
Sourcefn pad_end(&self, width: usize, pad: char) -> String
fn pad_end(&self, width: usize, pad: char) -> String
Pads the end of the string with pad to width characters.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".