Remove whitespace from a string in C++
This post will discuss how to remove whitespace from a string in C++.
By default, the following characters are considered whitespace characters:
- space
' ' - line feed
'\n' - carriage return
'\r' - horizontal tab
'\t' - form feed
'\f' - vertical tab
'\v'
1. Using std::remove_if function
The standard solution is to use the std::remove_if algorithm to remove whitespace characters from std::string using the Erase-remove idiom technique. Since the std::remove_if algorithm does not actually remove characters from the string but move all non-whitespace characters to the front and returns an iterator pointing to where the end should be. We can then delete the whitespace characters with a call to std::erase.
std::remove_if expects a predicate that determines which characters to remove from the string. There are many ways to specify the predicate that removes whitespace characters:
⮚ ::isspace
We can use isspace() defined in the cctype header, which checks for whitespace characters classified by the currently installed C locale.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <cctype> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end()); std::cout << s; return 0; } |
Output:
HelloWorld
⮚ std::isspace
Instead of relying on the whitespace characters classified by the C locale, we can use std::isspace defined in header locale, which classify the whitespace character with the specified locale’s ctype facet.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> #include <locale> #include <functional> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), std::bind(std::isspace<char>, std::placeholders::_1, std::locale::classic() )), s.end()); std::cout << s; return 0; } |
With C++11, we can use lambdas instead of std::bind:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), [](char &c) { return std::isspace<char>(c, std::locale::classic()); }), s.end()); std::cout << s; return 0; } |
⮚ Custom predicate
Instead of using ::isspace or std::isspace, we can even write a custom predicate that returns true if the character is classified as a whitespace character and false otherwise. There are many ways to achieve that in C++:
⮚1. Unary Function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isSpace(unsigned char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); } int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), isSpace), s.end()); std::cout << s; return 0; } |
⮚2. Object of a class implementing () operator
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> struct isSpace { bool operator()(unsigned c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); } }; int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), isSpace()), s.end()); std::cout << s; return 0; } |
⮚3. Lambda
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string s = "Hello \n\nWorld"; s.erase(std::remove_if(s.begin(), s.end(), [](char c) { return (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f'); }), s.end()); std::cout << s; return 0; } |
2. Using std::regex_replace function
With C++11, we can also use std::regex_replace for this. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <regex> int main() { std::string s = "Hello \n\nWorld"; std::regex r("\\s+"); s = std::regex_replace(s, r, ""); std::cout << s; return 0; } |
3. Using Boost Library
Finally, we can go with boost::algorithm::erase_all, which removes all the occurrences of the space string from the input.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <boost/algorithm/string/erase.hpp> int main() { std::string s = "Hello World"; boost::algorithm::erase_all(s, " "); std::cout << s; return 0; } |
That’s all about removing whitespace from a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)