Expand description
This library provides some functions that convert string cases between camelCase, COBOL-CASE,
kebab-case, MACRO_CASE, PascalCase, snake_case and Train-Case.
And this library also provides a trait Caser
which enables strings to convert themselves
to their cases by their own methods.
Basically, these functions only target ASCII uppercase and lowercase letters for capitalization. All characters other than ASCII uppercase and lowercase letters and ASCII numbers are removed as word separators.
If you want to use some symbols as separators, specify those symbols in the separators
field
of Options
struct and use the 〜case_with_options
function for the desired case.
If you want to retain certain symbols and use everything else as separators, specify those
symbols in keep
field of Options
struct and use the 〜case_with_options
function for the
desired case.
Additionally, you can specify whether to place word boundaries before and/or after
non-alphabetic characters with conversion options.
This can be set using the separate_before_non_alphabets
and separate_after_non_alphabets
fields in the Options
struct.
The 〜_case
functions that do not take Options
as an argument only place word boundaries
after non-alphabetic characters.
In other words, they behave as if separate_before_non_alphabets = false
and
separate_after_non_alphabets = true
.
§Install
In Cargo.toml
, write this crate as a dependency.
[dependencies]
stringcase = "0.4.0"
§Usage
The functions in this crate can be used as follows:
use stringcase::snake_case;
fn main() {
let input = "fooBar123Baz";
let snake = snake_case(input);
assert_eq!(snake, "foo_bar123_baz");
}
If you want the conversion to behave differently, use 〜_case_with_options
.
use stringcase::{snake_case_with_options, Options};
fn main() {
let opts = Options{separate_before_non_alphabets: true, ..Default::default()};
let input = "fooBar123Baz";
let snake = snake_case_with_options(input, &opts);
assert_eq!(snake, "foo_bar_123_baz");
}
And by bringing Caser
with use
declaration, it will be able to execute
methods of strings, String
or &str
, to convert to their cases.
use stringcase::{Caser, Options};
fn main() {
let input = "fooBar123Baz";
let snake = input.to_snake_case();
assert_eq!(snake, "foo_bar123_baz");
let opts = Options{separate_before_non_alphabets: true, ..Default::default()};
let snake = input.to_snake_case_with_options(&opts);
assert_eq!(snake, "foo_bar_123_baz");
}
Structs§
- Options
- Is a struct that represents options for case conversion of strings.
Traits§
- Caser
Caser
is the trait to attach methods for converting strings&str
andString
to various cases.
Functions§
- camel_
case - Converts the input string to camel case.
- camel_
case_ with_ keep Deprecated - Converts the input string to camel case with the specified characters to be kept.
- camel_
case_ with_ options - Converts the input string to camel case with the specified options.
- camel_
case_ with_ sep Deprecated - Converts the input string to camel case with the specified separator characters.
- cobol_
case - Converts the input string to cobol case.
- cobol_
case_ with_ keep Deprecated - Converts the input string to cobol case with the specified characters to be kept.
- cobol_
case_ with_ nums_ as_ word Deprecated - Converts the input string to cobol case.
- cobol_
case_ with_ options - Converts the input string to cobol case with the specified options.
- cobol_
case_ with_ sep Deprecated - Converts the input string to cobol case with the specified separator characters.
- kebab_
case - Converts the input string to kebab case.
- kebab_
case_ with_ keep Deprecated - Converts the input string to kebab case with the specified characters to be kept.
- kebab_
case_ with_ nums_ as_ word Deprecated - Converts the input string to kebab case.
- kebab_
case_ with_ options - Converts the input string to kebab case with the specified options.
- kebab_
case_ with_ sep Deprecated - Converts the input string to kebab case with the specified separator characters.
- macro_
case - Converts the input string to macro case.
- macro_
case_ with_ keep Deprecated - Converts the input string to macro case with the specified characters to be kept.
- macro_
case_ with_ nums_ as_ word Deprecated - Converts the input string to macro case.
- macro_
case_ with_ options - Converts the input string to macro case with the specified options.
- macro_
case_ with_ sep Deprecated - Converts the input string to macro case with the specified separator characters.
- pascal_
case - Converts the input string to pascal case.
- pascal_
case_ with_ keep Deprecated - Converts the input string to pascal case with the specified characters to be kept.
- pascal_
case_ with_ options - Converts the input string to pascal case with the specified options.
- pascal_
case_ with_ sep Deprecated - Converts the input string to pascal case with the specified separator characters.
- snake_
case - Converts the input string to snake case.
- snake_
case_ with_ keep Deprecated - Converts the input string to snake case with the specified characters to be kept.
- snake_
case_ with_ nums_ as_ word Deprecated - Converts the input string to snake case.
- snake_
case_ with_ options - Converts the input string to snake case with the specified options.
- snake_
case_ with_ sep Deprecated - Converts the input string to snake case with the specified separator characters.
- train_
case - Converts the input string to train case.
- train_
case_ with_ keep Deprecated - Converts the input string to train case with the specified characters to be kept.
- train_
case_ with_ nums_ as_ word Deprecated - Converts the input string to train case.
- train_
case_ with_ options - Converts the input string to train case with the specified options.
- train_
case_ with_ sep Deprecated - Converts the input string to train case with the specified separator characters.