Remove all whitespace from string
17,823
Solution 1
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()
}
Solution 2
A good option is to use split_whitespace
and then collect to a string :
fn remove_whitespace(s: &str) -> String {
s.split_whitespace().collect()
}
Solution 3
Actually I found a shorter approach
fn no_space(x : String) -> String{
x.replace(" ", "")
}
Related videos on Youtube
Comments
-
Magix about 2 years
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?
-
Admin almost 5 yearsUse
\s+
replace with nothing.inclusion of the 'regex' crate, ...
-
Justin almost 5 yearsI think this is a good question. This isn't a duplicate (closest is 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.
-
Stargateur almost 5 years@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 stackoverflow.com/questions/37792471/….
-
E_net4 the comment flagger almost 5 years@Justin We usually expect a greater amount of research before asking. For one, searching the standard library alone for
replace
returnsstd::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. -
Justin almost 5 years@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 almost 5 years@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 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.
-
Magix almost 5 years@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
-
Stargateur@Justin note that stackoverflow has changed a lot between now and 2011 also as e_net4 said this question will probably get upvote in the future, we agree this question is not useless just it's still trivial and easy to answer. This kind of question is more suitable to be self answered. Asking such thing that just a look to string documentation answer is just low research effort. Even if Magix said s/he search, that still a fail to find basic answer. Also I downvote on the first version of the question that you almost all rewrite.
-
-
E_net4 the comment flagger almost 5 years@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 aString
from the iterator of characters, but this will be the only allocation either way. -
hellow almost 5 years
-
Magix over 3 yearsNice ! 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()
-
sreenivas over 2 yearsInitially I thought, we should use single quotes. Isn't whitespace a character ?