Trait capitalize::Capitalize

source ·
pub trait Capitalize: AsRef<str> {
    // Required methods
    fn capitalize(&self) -> String;
    fn capitalize_words(&self) -> String;
    fn capitalize_first_only(&self) -> String;
    fn capitalize_last_only(&self) -> String;
}
Expand description

It’s implemented for all types that implement AsRef<str>.

Required Methods§

source

fn capitalize(&self) -> String

First character to title case and the rest to lower case. This means that characters like digraphs will only have their first letter capitalized, instead of the full character.

Behavior is like Python’s str.capitalize. Also, it uses char::to_uppercase under the hood, then read its doc. That relies on Unicode to change to upper case.

§Examples
assert_eq!("✨ Hello World".capitalize(), "✨ hello world");
assert_eq!("ñandu".capitalize(), "Ñandu");
assert_eq!("こんにちは世界".capitalize(), "こんにちは世界");
source

fn capitalize_words(&self) -> String

Available on crate feature nightly only.

Split by space into words, then for each word change the first character to title case and the rest to lower case. This means that characters like digraphs will only have their first letter capitalized, instead of the full character.

It uses [char.to_uppercase()] under the hood, then read its doc. That relies on Unicode to change to upper case.

§Examples
assert_eq!("✨ hello world".capitalize_words(), "✨ Hello World");
assert_eq!("ñandu".capitalize_words(), "Ñandu");
assert_eq!("こんにちは世界".capitalize_words(), "こんにちは世界");
source

fn capitalize_first_only(&self) -> String

First character to upper case and the rest will remain the same.

It uses [char.to_uppercase()] under the hood, then read its doc. That relies on Unicode to change to upper case.

§Examples
assert_eq!("hello World".capitalize_first_only(), "Hello World");
assert_eq!("✨ hello World".capitalize_first_only(), "✨ hello World");
source

fn capitalize_last_only(&self) -> String

The last character to upper case and the rest will remain the same.

It uses [char.to_uppercase()] under the hood, then read its doc. That relies on Unicode to change to upper case.

§Examples
assert_eq!("✨ Hello World".capitalize_last_only(), "✨ Hello WorlD");
assert_eq!("Hello World ✨".capitalize_last_only(), "Hello World ✨");
assert_eq!("hello world".capitalize_last_only(), "hello worlD");

Implementors§

source§

impl<T: AsRef<str>> Capitalize for T