#convert-string #kebab-case #camel-case #snake-case #pascal-case #string-formatting #case

stringcase

Converts string cases between camelCase, COBOL-CASE, kebab-case, and so on

5 unstable releases

0.3.0 Aug 12, 2024
0.2.1 Feb 26, 2024
0.2.0 Feb 25, 2024
0.1.1 Feb 23, 2024
0.1.0 Feb 18, 2024

#190 in Text processing

Download history 190/week @ 2024-08-19 161/week @ 2024-08-26 24/week @ 2024-09-02 7/week @ 2024-09-09 83/week @ 2024-09-16 143/week @ 2024-09-23 184/week @ 2024-09-30 92/week @ 2024-10-07 194/week @ 2024-10-14 104/week @ 2024-10-21 308/week @ 2024-10-28 170/week @ 2024-11-04 186/week @ 2024-11-11 1611/week @ 2024-11-18 1858/week @ 2024-11-25 2283/week @ 2024-12-02

5,938 downloads per month
Used in 35 crates (2 directly)

MIT license

180KB
4K SLoC

stringcase-rust crate.io doc.rs CI Status MIT License

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 makes strings enable to convert their cases by their own methods.

Basically, these functions targets the upper and lower cases of only ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

To limit characters using as separators, the functions named like *_with_sep are provided, and to keep specified characters, the functions named like *_with_keep are provided.

In this crate, the default behavior of the conversion functions is to insert a separator after a sequence of numbers and symbols, but not before them. (For example, snake_case("abc123def") ==> "abc123_def") However, for cases where you want to insert a separator before the sequence as well, the functions names like *_with_nums_as_word are provided. (For example, snake_case_with_nums_as_word("abc123def") ==> "abc_123_def")

Install

In Cargo.toml, write this crate as a dependency.

[dependencies]
stringcase = "0.3.0"

Usage

The function contained in this crate can be executed as follows:

use stringcase::camel_case;

fn main() {
    let input = "foo-bar-baz";
    let camel = camel_case(input);
    assert_eq!(camel, "fooBarBaz");
}

And by bringing Caser with use declaration, it will be able to execute methods of strings, String or &str, to convert their cases.

use stringcase::Caser;

func main() {
    let input = "foo-bar-baz";
    let camel = input.to_camel_case();
    assert_eq!(camel, "fooBarBaz");
}

Supporting Rust versions

This library supports Rust 1.56.1 or later.

% cargo msrv
Fetching index
Determining the Minimum Supported Rust Version (MSRV) for toolchain x86_64-apple-darwin
Using check command cargo check
Check for toolchain '1.66.1-x86_64-apple-darwin' succeeded
Check for toolchain '1.61.0-x86_64-apple-darwin' succeeded
Check for toolchain '1.58.1-x86_64-apple-darwin' succeeded
Check for toolchain '1.57.0-x86_64-apple-darwin' succeeded
Check for toolchain '1.56.1-x86_64-apple-darwin' succeeded
   Finished The MSRV is: 1.56.1   █████████████████████████████████████ 00:00:20

License

Copyright (C) 2024 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

No runtime deps