How to remove all occurrences of word from a string in Rust

2 Answers

0 votes
fn main() {
    let mut s = String::from("rust golang c# java c c++ java java python");
    let toremove = "java";
  
    s = s.replace(toremove, "");
      
    println!("{}", s);
}
  
      
      
/*
run:
  
rust golang c#  c c++   python
  
*/

 



answered Oct 13, 2024 by avibootz
edited Oct 14, 2024 by avibootz
0 votes
use regex::Regex;
 
fn main() {
    let mut s = "rust golang c# java c c++ java java python".to_string();
    let toremove = "java";
 
    s = s.replace(toremove, "");
     
    let whitespace_regex = Regex::new(r"\s+").unwrap();
    s = whitespace_regex.replace_all(&s, " ").to_string();
     
    println!("{}", s);
}
 
     
     
/*
run:
 
rust golang c# c c++ python
 
*/

 



answered Oct 14, 2024 by avibootz
...