Home
Rust
String Between, Before and After
Updated Oct 24, 2024
Dot Net Perls
Between, before, after. Parsing strings is always a challenge when learning a programming language. But most languages have similar methods and parsing concepts.
Rust parsing. In Rust we invoke find and rfind to locate strings. With these functions we can get substrings between, before and after other strings.
Substring
find
Input and output. Suppose we are reading lines from a log file, or other text file that has formatted data. Here we see the required output for a certain line of input.
Input = "DEFINE:A=TWO" Between("DEFINE:", "=TWO") = "A"
Example program. In Rust, we call find() and rfind and place these calls in if-statements. We use string slices to return the desired substrings.
Note Between() indicates that the return value is borrowed from the "value" string. The result is a substring of value.
String Length
Note 2 Before is similar to between, but simpler—before() does not find an "after" part. It also uses string slice syntax.
Next After() is the same thing, but in reverse, as the before function. It returns the string slice after a certain substring.
Tip For understanding borrowing in Rust, it is a good idea to try to develop methods like these and follow the Rust compiler's hints.
lifetime
fn between<'value>(value: &'value str, a: &str, b: &str) -> &'value str { // Find the two strings. if let Some(pos_a) = value.find(a) { if let Some(pos_b) = value.rfind(b) { // Return the part in between the 2 strings. let adjusted_pos_a = &pos_a + a.len(); if adjusted_pos_a < pos_b { return &value[adjusted_pos_a..pos_b]; } } } return ""; } fn before<'value>(value: &'value str, a: &str) -> &'value str { // Find the string and return the part before. if let Some(pos_a) = value.find(a) { return &value[0..pos_a]; } return ""; } fn after<'value>(value: &'value str, a: &str) -> &'value str { // Find the string and return the part after. if let Some(pos_a) = value.rfind(a) { let adjusted_pos_a = pos_a + a.len(); if adjusted_pos_a < value.len() { return &value[adjusted_pos_a..]; } } return ""; } fn main() { let test = "DEFINE:A=TWO"; println!("{}", between(test, "DEFINE:", "=")); println!("{}", between(test, ":", "=")); println!("{}", before(test, ":")); println!("{}", before(test, "=")); println!("{}", after(test, ":")); println!("{}", after(test, "DEFINE:")); println!("{}", after(test, "=")); }
A A DEFINE DEFINE:A A=TWO A=TWO TWO
Summary. With Rust, borrowing variables is the most unique part of developing between, before and after functions. We have to indicate what things own other things.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Oct 24, 2024 (edit link).
Home
Changes
© 2007-2025 Sam Allen