To remove a specific character from a string in Rust, you can use the `replace` method.
In this example,
str
with a value of "Hello World"
.replace
method to replace all occurrences of 'l' with an empty string in str
.replace
method returns a new string with the specified characters replaced.str
.fn main() {
let mut str = String::from("Hello World");
str = str.replace("l", "");
println!("Modified string: {}", str);
}
Modified string: Heo Word
In this example,
str
with a value of "apple"
.replace
method to replace all occurrences of 'a' with an empty string in str
.replace
method returns a new string with the specified characters replaced.str
.fn main() {
let mut str = String::from("apple");
str = str.replace("a", "");
println!("Modified string: {}", str);
}
Modified string: pple
In this tutorial, we learned How to Remove specific Character from a String in Rust language with well detailed examples.