31

How do I remove all whitespace from a string? I can think of some obvious methods such as looping over the string and removing each whitespace character, or using regular expressions, but these solutions are not that expressive or efficient. What is a simple and efficient way to remove all whitespace from a string?

Justin
  • 22,898
  • 12
  • 90
  • 138
Magix
  • 4,359
  • 6
  • 22
  • 46
  • Use `\s+` replace with nothing. `inclusion of the 'regex' crate, ...` –  Jul 16 '19 at 19:00
  • 5
    I think this is a good question. This isn't a duplicate (closest is https://stackoverflow.com/q/37792471/1896169 , which you could argue this is a duplicate of, but it's clearly hard to find this one if you search for terms in this question), and at the least, it is a clear, concrete problem. Also, this is definitely not "too broad." It's a very clear defined problem, and a single problem. – Justin Jul 16 '19 at 19:55
  • @Justin Still look like code request, this is a trivial question, any basic search would have answer this, I think we could even close as duplicate https://stackoverflow.com/questions/37792471/removing-elements-from-a-vec-based-on-some-condition. – Stargateur Jul 16 '19 at 21:00
  • 1
    @Justin We usually expect a greater amount of research before asking. For one, searching the standard library alone for [`replace`](https://doc.rust-lang.org/std/?search=replace) returns `std::str::replace` as the third entry, which would have also done the job well. This is the kind of question that may well become useful, but will just have to prove its own usefulness with time. – E_net4 - Krabbe mit Hüten Jul 16 '19 at 21:17
  • 4
    @Stargateur I very much disagree. Basic searches for removing whitespace from a string, erasing whitespace from a string, etc, don't turn up anything useful for Rust. You get other highly upvoted things for other languages, but not for Rust. – Justin Jul 16 '19 at 21:30
  • 2
    @E_net4isoutofcommentflags I have to disagree. How would you know to search for "replace"? This operation isn't obviously a replace; it's an erase/removal. When I did the basic research for this question, I found this [highly upvoted C# question](https://stackoverflow.com/q/6219454/1896169) which is practically the same thing. The only way I was able to turn up useful results for Rust was by having the knowledge on what terms to remove from the search and what similar things to search for---at that level, a new Stack Overflow question is entirely appropriate. – Justin Jul 16 '19 at 21:33
  • 2
    @Stargateur I searched before asking, and I asked specifically because there was no good, well-defined answer for Rust. Also, I believe over time this question could become a comparison between the different methods if many good answers exist – Magix Jul 16 '19 at 21:34

3 Answers3

44

If you want to modify the String, use retain. This is likely the fastest way when available.

fn remove_whitespace(s: &mut String) {
    s.retain(|c| !c.is_whitespace());
}

If you cannot modify it because you still need it or only have a &str, then you can use filter and create a new String. This will, of course, have to allocate to make the String.

fn remove_whitespace(s: &str) -> String {
    s.chars().filter(|c| !c.is_whitespace()).collect()
}
APerson
  • 7,746
  • 8
  • 34
  • 48
JayDepp
  • 945
  • 9
  • 10
19

A good option is to use split_whitespace and then collect to a string :

fn remove_whitespace(s: &str) -> String {
    s.split_whitespace().collect()
}
Magix
  • 4,359
  • 6
  • 22
  • 46
  • 2
    @Magix `split_whitespace` does not consume the string, so making the function receive a string slice is the right thing to do. `collect` will create a `String` from the iterator of characters, but this will be the only allocation either way. – E_net4 - Krabbe mit Hüten Jul 17 '19 at 08:12
  • https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6b86c8a9cb6138c0d554110c3e1c3822 – hellow Jul 17 '19 at 08:43
4

Actually I found a shorter approach

fn no_space(x : String) -> String{
  x.replace(" ", "")
}
Jurij Jazdanov
  • 1,228
  • 8
  • 11
  • 3
    Nice ! This only works with normal space characters, though. In practice, there is a number of whitespace characters that one would want to remove, which are found using `is_whitespace()` – Magix Jan 05 '21 at 16:43
  • Initially I thought, we should use single quotes. Isn't whitespace a character ? – sreenivas Oct 05 '21 at 09:45