rust_string_utils 0.1.11

String utilities for rust based on org.apache.commons.lang3
Documentation
rust_string_utils-0.1.11 has been yanked.

Rust String Utilities

Description

rust_string_utils is a Rust library providing various utility functions for string manipulation.

Repository

GitHub Repository

Crates.io

https://crates.io/crates/rust_string_utils

Installation

Add the following to your Cargo.toml:

[dependencies]
rust_string_utils = "0.1.11"

Usage

Here are some examples of how to use the functions provided by this library:

Check if a string is empty

use rust_string_utils::is_empty;

let result = is_empty(String::from(""));
assert_eq!(result, true);

Check if a string is blank

use rust_string_utils::is_blank;

let result = is_blank(String::from("   "));
assert_eq!(result, true);

Reverse a string

use rust_string_utils::reverse;

let result = reverse(String::from("abcde"));
assert_eq!("edcba", result);

Check if a string starts with a prefix

use rust_string_utils::start_with;

let result = start_with(String::from("abcde"), String::from("a"));
assert_eq!(true, result);

Checks if the given string ends with the specified suffix.

use your_crate::end_with;

let result = end_with("abcde".to_string(), "e".to_string());
assert_eq!(result, true);

Join characters with a delimiter

use rust_string_utils::join_char;

let result = join_char(vec!['a', 'b', 'c'], ',');
assert_eq!("a,b,c", result);

Replace a string in a string

let result = replace(String::from("hello world"), String::from("world"), String::from("Rust"));
assert_eq!(result, "hello Rust");

Replace a character in a string

let result = replace_char(String::from("hello world"), 'o', 'O');
assert_eq!(result, "hellO wOrld");

Count the number of occurrences of a character in a string

let count = count_matches(String::from("hello world"), 'o');
assert_eq!(count, 2);

Triple a string

let result = trip(String::from("a b c "));
assert_eq!("abc", result);

Swap case of a string

let result = swap_case(String::from("Hello World"));
assert_eq!("hELLO wORLD", result);

Convert a timestamp to a human-readable date

let formatted_date = timestamp_to_string(1618033988000, "%Y-%m-%d %H:%M:%S");
assert_eq!(formatted_date, "2021-04-10 05:53:08");

Convert a byte array to string

let bytes = vec![104, 101, 108, 108, 111];
let result = bytes_to_string(bytes);
assert_eq!(result, "hello");

Rotate characters in a string

use rust_string_utils::rotate;

let result = rotate("abcdefg", 2);
assert_eq!(result, "fgabcde");

Split a string by whitespace

use rust_string_utils::split;

let result = split("abc def");
assert_eq!(result, vec!["abc", "def"]);

Split a string by specified separator characters

use rust_string_utils::split_with_separator;

let result = split_with_separator("abc,def".to_string(), ",".to_string());
assert_eq!(result, vec!["abc", "def"]);

Compare two strings lexicographically

use rust_string_utils::compare;

let result = compare("abc".to_string(), "abc".to_string());
assert_eq!(result, 0);

Compare two strings lexicographically, ignoring case differences

use rust_string_utils::compare_ignore_case;

let result = compare_ignore_case("abc".to_string(), "Abc".to_string());
assert_eq!(result, 0);

Compares two strings for equality.

use your_crate::equals;

let result = equals("hello".to_string(), "hello".to_string());
assert_eq!(result, true);

Compares two strings for equality, ignoring case.

use your_crate::equals_ignore_case;

let result = equals_ignore_case("Hello".to_string(), "hello".to_string());
assert_eq!(result, true);

Finds the index of the first occurrence of the specified substring.

use your_crate::index_of;
let index = index_of("hello".to_string(), "l".to_string());
assert_eq!(index, 2);

Finds the index of the first occurrence of the specified substring starting from a given index.

use your_crate::index_of_from;

let index = index_of_from("hello".to_string(), "l".to_string(), 3);
assert_eq!(index, 3);

Finds the index of the last occurrence of the specified substring.

use your_crate::last_index_of;

let index = last_index_of("hello".to_string(), "l".to_string());
assert_eq!(index, 3);

Overlays part of a string with another string

use rust_string_utils::overlay;
let result = overlay("abcdef".to_string(), "zzzz".to_string(), 2, 4);
assert_eq!(result, "abzzzzef");

License

This project is licensed under either of