The ends_with()
method is used to check if a particular substring ends a particular string. For example, with this method, we can check if the string, "Edpresso"
, ends with "presso"
.
string.ends_with(subString)
string
: This is the string we want to check if it ends with another string—the substring.subString
: This is the substring that we want to check if it ends with the string, string
.It returns true
if the string ends with the specified substring. Otherwise, it returns false
.
fn main() {// create some stringslet str1 = "Edpresso";let str2 = "Educative";let str3 = "Rust";let str4 = "Educative is the best platform!";// create strings to checklet end1 = "presso";let end2 = "cative";let end3 = "R";let end4 = "best platform!";// check if strings ends with the sub-stringsprintln!(" {} ends with {}: {}",str1, end1, str1.ends_with(end1));println!(" {} ends with {}: {}",str2, end2, str2.ends_with(end2));println!(" {} ends with {}: {}",str3, end3, str3.ends_with(end3));println!(" {} ends with {}: {}",str4, end4, str4.ends_with(end4));}
ends_with()
method to check if the strings end with the specified substrings and print the results to the console.