rustrust string contains
A String
in Rust is a UTF-8 encoded, growable bit of text. It is a collection of char
s, and is one of the most commonly used data types in Rust.
let mut my_string = String::new();
my_string.push_str("Hello, world!");
println!("{}", my_string);
Output example
Hello, world!
The String
type has several methods that can be used to check if it contains a certain substring. These include:
contains()
: Checks if aString
contains a given substring.find()
: Returns the index of the first character of a given substring in aString
.rfind()
: Returns the index of the last character of a given substring in aString
.
These methods can be used to check if a String
contains a given substring. For example:
let my_string = String::from("Hello, world!");
assert!(my_string.contains("world"));
assert_eq!(my_string.find("world"), 7);
assert_eq!(my_string.rfind("world"), 7);
The contains()
method returns a bool
indicating whether the String
contains the given substring. The find()
and rfind()
methods return the index of the first and last character of the substring, respectively.
Helpful links
More of Rust
- How to replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to compile a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to convert the keys of a Rust HashMap to a vector?
- How to match the end of a line in a Rust regex?
- How to convert a Rust slice of u8 to u32?
- How to get a capture group using Rust regex?
- How to use groups in a Rust regex?
See more codes...