How to remove duplicate spaces from a string in Rust

1 Answer

0 votes
use regex::Regex;

fn main() {
    let mut s = "rust     golang      c#   java c    c++   python".to_string();
    
    let whitespace_regex = Regex::new(r"\s+").unwrap();
    s = whitespace_regex.replace_all(&s, " ").to_string();
    
    println!("{}", s);
}

    
    
/*
run:

rust golang c# java c c++ python

*/

 



answered Oct 14, 2024 by avibootz
...