Skip to content

words

Splits s into its words, keeping their original casing.

A word ends at any non-alphanumeric character, before an uppercase letter that follows a lowercase letter or a digit (userName, v2Beta), and before the last capital of an acronym run (HTMLParser gives HTML, Parser). Use one of the *_case helpers instead if you want the words lowercased and rejoined.

use helpers4::string::words;

Cargo feature string (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features string

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.7", default-features = false, features = ["string"] }
pub fn words(s: &str) -> Vec<&str>
ParameterTypeDescription
s&strThe text to split.

Vec<&str> — The words, in order, with no separators or empty entries.

use helpers4::string::words;

assert_eq!(words("hello world"), vec!["hello", "world"]);
assert_eq!(words("camelCaseString"), vec!["camel", "Case", "String"]);
assert_eq!(words("SCREAMING_SNAKE"), vec!["SCREAMING", "SNAKE"]);
assert_eq!(words("foo123bar"), vec!["foo123bar"]);
assert!(words("   ").is_empty());

src/string/words.rs