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

stringcase

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

6 releases (3 breaking)

0.4.0 Mar 2, 2025
0.3.0 Aug 12, 2024
0.2.1 Feb 26, 2024
0.1.1 Feb 23, 2024

#102 in Text processing

Download history 1721/week @ 2024-11-19 1902/week @ 2024-11-26 2584/week @ 2024-12-03 3633/week @ 2024-12-10 4520/week @ 2024-12-17 3709/week @ 2024-12-24 3251/week @ 2024-12-31 6401/week @ 2025-01-07 6663/week @ 2025-01-14 6154/week @ 2025-01-21 6713/week @ 2025-01-28 6690/week @ 2025-02-04 7470/week @ 2025-02-11 7255/week @ 2025-02-18 8678/week @ 2025-02-25 6185/week @ 2025-03-04

30,598 downloads per month
Used in 55 crates (2 directly)

MIT license

560KB
14K 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 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 executed 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 themselves 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");
}

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.71.1-x86_64-apple-darwin' succeeded
Check for toolchain '1.63.0-x86_64-apple-darwin' succeeded
Check for toolchain '1.59.0-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:29

License

Copyright (C) 2024-2025 Takayuki Sato

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

No runtime deps