| Crates.io | substr_rs |
| lib.rs | substr_rs |
| version | 0.3.0 |
| created_at | 2025-07-14 13:11:00.053269+00 |
| updated_at | 2025-07-20 08:02:23.885069+00 |
| description | Easy substring extraction |
| homepage | |
| repository | https://github.com/f42h/substr_rs |
| max_upload_size | |
| id | 1751680 |
| size | 16,649 |
This Rust library provides a Substring struct with methods
to extract substrings from a given string based on specified
delimiters, regular expressions, or index ranges.
character or string delimiters.regular expressions.start and end indices.To use the Substring struct,
cargo add substr_rs
substr_rs = "0.3.0"
use substr_rs::Substring;
fromExtracts a substring from strval starting after the first occurrence
of start and ending before the first occurrence of end.
pub fn from<T: ToString>(strval: &str, start: T, end: T) -> Option<String>
strval: The input string from which the substring will be extracted.start: The starting delimiter, where the substring begins.end: The ending delimiter, where the substring ends.Option<String> containing the extracted substring if both delimiters
are found; otherwise, None.
from_regexExtracts a substring from strval starting after the first match of the
start regex and ending before the first match of the end regex.
pub fn from_regex(strval: &str, start: &str, end: &str) -> Option<String>
strval: The input string from which the substring will be extracted.start: A regex pattern marking where the substring begins.end: A regex pattern marking where the substring ends.Option<String> containing the extracted substring if both regex patterns
match in the correct order; otherwise, None.
from_indexExtracts a substring from strval starting at the given start index and
ending at the given end index.
pub fn from_index(strval: &str, start: usize, end: usize) -> Option<String>
strval: The input string from which the substring will be extracted.start: The starting index where the substring begins.end: The ending index where the substring ends.Option<String> containing the extracted substring if the indices are valid;
otherwise, None.
let string_value = "SomeSubstringData";
let start_char: char = 'e';
let end_char: char = 'D';
if let Some(substr) = Substring::from(string_value, start_char, end_char) {
println!(
"The substring of `{}` between the characters `{}` and `{}` is {}",
string_value, start_char, end_char,
substr
);
}
// Output:
// The substring of `SomeSubstringData` between the characters `e` and `D` is Substring
let string_value = "<script id='main'>console.log('Hello, World!');</script>";
let start_pattern = "<script[^>]*>";
let end_pattern = "</script";
if let Some(substr) = Substring::from_regex(string_value, start_pattern, end_pattern) {
println!(
"The substring of `{}` between the patterns `{}` and `{}` is {}",
string_value, start_pattern, end_pattern,
substr
);
}
// Output:
// The substring of `<script id='main'>console.log('Hello, World!');</script>` between the
// patterns `<script[^>]*>` and `</script` is console.log('Hello, World!');
This project is published under the MIT License. See the LICENSE file for more details.