Home
Rust
String Between, Before and After
This page was last reviewed on 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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 24, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.