To get the substring between two specific characters in Rust, you can use methods like `find` and slicing with the indices of the characters.
In this example,
url
with a value of "https://example.com/page?query=1"
.find
method to find the positions of '/'
and '?'
.'/'
plus 1 and the end index as the position of '?'
.fn main() {
let url = "https://example.com/page?query=1";
let start = url.find('/') + 1;
let end = url.find('?');
if let Some(start) = start {
if let Some(end) = end {
let substring = &url[start..end];
println!("Substring between '/' and '?': {}", substring);
}
}
}
Substring between '/' and '?': page
In this example,
str
with a value of "Hello,World!"
.find
method to find the positions of ','
and '!'
.','
plus 1 and the end index as the position of '!'
.fn main() {
let str = "Hello,World!";
let start = str.find(',') + 1;
let end = str.find('!');
if let Some(start) = start {
if let Some(end) = end {
let substring = &str[start..end];
println!("Substring between ',' and '!': {}", substring);
}
}
}
Substring between ',' and '!': World
In this tutorial, we learned How to get Substring between two Specific Characters in Rust language with well detailed examples.