Skip to main content

Str

Trait Str 

Source
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§

Source

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.

Source

fn kebab(&self) -> String

Converts the string to kebab case, so BlogPostTitle becomes blog-post-title.

Source

fn studly(&self) -> String

Converts the string to studly case, also called Pascal case, splitting words at whitespace, -, and _. blog_post_title becomes BlogPostTitle.

Source

fn camel(&self) -> String

Converts the string to camel case, so blog_post_title becomes blogPostTitle.

Source

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.

Source

fn slug_with(&self, separator: &str) -> String

Returns a slug with words joined by separator, so "Hello World".slug_with("_") returns hello_world.

Source

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.

Source

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.

Source

fn upper_first(&self) -> String

Converts the first letter to uppercase, so taylor otwell becomes Taylor otwell.

Source

fn lower_first(&self) -> String

Converts the first letter to lowercase, so Taylor becomes taylor.

Source

fn split_upper(&self) -> Vec<&str>

Splits the string before each uppercase letter, so laravelFramework becomes ["laravel", "Framework"].

Source

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é.

Source

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.

Source

fn ascii(&self) -> String

Transliterates the string to ASCII, so Ünïcödé becomes Unicode.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

fn take(&self, count: usize) -> &str

Returns the first count characters, so "Build something amazing!".take(5) returns Build.

Source

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.

Source

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/.

Source

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.

Source

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.

Source

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.

Source

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!.

Source

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.

Source

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.

Source

fn limit_with(&self, limit: usize, end: &str) -> String

Cuts the string to limit characters and ends it with end when it was longer.

Source

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....

Source

fn words_with(&self, words: usize, end: &str) -> String

Cuts the string to words words and ends it with end when there were more.

Source

fn word_count(&self) -> usize

Returns the number of words, counting words separated by whitespace.

Source

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.

Source

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.

Source

fn squish(&self) -> String

Trims the string and replaces each run of whitespace with a single space, so " laravel framework " becomes laravel framework.

Source

fn deduplicate(&self, character: char) -> String

Replaces each run of character with a single character, so "The Laravel Framework".deduplicate(' ') returns The Laravel Framework.

Source

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.

Source

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.

Source

fn pad_end(&self, width: usize, pad: char) -> String

Pads the end of the string with pad to width characters.

Source

fn pad_both(&self, width: usize, pad: char) -> String

Pads both sides of the string with pad to width characters, putting any extra character at the end. "James".pad_both(10, '_') returns __James___.

Source

fn digits(&self) -> String

Returns only the string’s ASCII digits, so "(555) 123-4567".digits() returns 5551234567.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Str for str

Source§

fn snake(&self) -> String

Source§

fn kebab(&self) -> String

Source§

fn studly(&self) -> String

Source§

fn camel(&self) -> String

Source§

fn slug(&self) -> String

Source§

fn slug_with(&self, separator: &str) -> String

Source§

fn title(&self) -> String

Source§

fn headline(&self) -> String

Source§

fn upper_first(&self) -> String

Source§

fn lower_first(&self) -> String

Source§

fn split_upper(&self) -> Vec<&str>

Source§

fn plural(&self) -> String

Source§

fn singular(&self) -> String

Source§

fn ascii(&self) -> String

Source§

fn after(&self, search: &str) -> &str

Source§

fn after_last(&self, search: &str) -> &str

Source§

fn before(&self, search: &str) -> &str

Source§

fn before_last(&self, search: &str) -> &str

Source§

fn between(&self, from: &str, to: &str) -> &str

Source§

fn between_first(&self, from: &str, to: &str) -> &str

Source§

fn chop_start(&self, prefix: &str) -> &str

Source§

fn chop_end(&self, suffix: &str) -> &str

Source§

fn take(&self, count: usize) -> &str

Source§

fn start(&self, prefix: &str) -> String

Source§

fn finish(&self, cap: &str) -> String

Source§

fn replace_last(&self, search: &str, replace: &str) -> String

Source§

fn replace_start(&self, search: &str, replace: &str) -> String

Source§

fn replace_end(&self, search: &str, replace: &str) -> String

Source§

fn swap(&self, replacements: &[(&str, &str)]) -> String

Source§

fn resembles(&self, pattern: &str) -> bool

Source§

fn limit(&self, limit: usize) -> String

Source§

fn limit_with(&self, limit: usize, end: &str) -> String

Source§

fn words(&self, words: usize) -> String

Source§

fn words_with(&self, words: usize, end: &str) -> String

Source§

fn word_count(&self) -> usize

Source§

fn excerpt(&self, phrase: &str, radius: usize) -> Option<String>

Source§

fn word_wrap(&self, width: usize) -> String

Source§

fn squish(&self) -> String

Source§

fn deduplicate(&self, character: char) -> String

Source§

fn mask(&self, character: char, range: impl RangeBounds<usize>) -> String

Source§

fn pad_start(&self, width: usize, pad: char) -> String

Source§

fn pad_end(&self, width: usize, pad: char) -> String

Source§

fn pad_both(&self, width: usize, pad: char) -> String

Source§

fn digits(&self) -> String

Implementors§