rust string contains
To check if a Rust string contains a specific substring, you can use the contains
method provided by the str
type. Here's an example:
fn main() {
let my_string = "Hello, world!";
let substring = "world";
if my_string.contains(substring) {
println!("The string contains the substring.");
} else {
println!("The string does not contain the substring.");
}
}
In this example, the contains
method is called on the my_string
variable, passing the substring
as an argument. The method returns a boolean value indicating whether the substring is present in the string. Based on the result, you can perform the desired actions.
Note that the contains
method is case-sensitive. If you want to perform a case-insensitive search, you can convert both the string and the substring to lowercase or uppercase before calling the contains
method.
I hope this helps! Let me know if you have any further questions.