Replace consecutive whitespace with a single space in a string in C++
This post will discuss how to replace consecutive whitespace with a single space in a string in C++.
1. Using std::unique
The std::unique function is used to remove consecutive duplicates in the specified range. It takes a binary predicate to determine whether specified arguments are equivalent or not. It can be used as follows to remove all duplicate whitespaces.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <algorithm> bool isBothSpace(char const &lhs, char const &rhs) { return lhs == rhs && iswspace(lhs); } int main() { std::string str = " Hello World "; auto it = std::unique(str.begin(), str.end(), isBothSpace); str.erase(it, str.end()); std::cout << "|" << str << "|" << std::endl; // | Hello World | return 0; } |
The above code can be easily shortened using lambda expressions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = " Hello World "; auto it = std::unique(str.begin(), str.end(), [](char const &lhs, char const &rhs) { return (lhs == rhs) && (lhs == ' '); }); str.erase(it, str.end()); std::cout << "|" << str << "|" << std::endl; // | Hello World | return 0; } |
To create a copy of the string with duplicates removed, consider using the std::unique_copy algorithm:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <algorithm> #include <iterator> int main() { std::string str = " Hello World "; std::string s; std::unique_copy(str.begin(), str.end(), std::back_insert_iterator<std::string>(s), [](char &x, char &y) { return iswspace(x) && iswspace(y); }); std::cout << "|" << s << "|" << std::endl; // | Hello World | return 0; } |
2. Using Regex
Another option is to use a regular expression to replace consecutive whitespace with a single space. Starting with C++11, we can use std::regex_replace from the header <regex>. Its usage 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 str = " Hello World "; std::regex reg(R"(\s+)"); std::string s = std::regex_replace(str, reg, " "); std::cout << "|" << s << "|" << std::endl; // | Hello World | return 0; } |
Alternatively, we can use the boost::regex_replace algorithm by boost library. Here’s an example of its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <algorithm> #include <boost/regex.hpp> #include <boost/algorithm/string/regex.hpp> int main() { std::string str = " Hello World "; std::string s = boost::regex_replace(str, boost::regex("\\s+"), " "); std::cout << "|" << s << "|" << std::endl; // | Hello World | return 0; } |
3. Using std::istringstream
Finally, we can use std::istringstream to remove duplicate whitespace in a string. Here’s an example of its usage, which removes trailing and leading spaces as well:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> #include <sstream> std::string removeDuplicateSpaces(std::string const &str) { std::string s; std::string word; std::istringstream ss(str); while (ss >> word) { if (!s.empty()) { s += ' '; } s += word; } return s; } int main() { std::string str = " Hello World "; std::string s = removeDuplicateSpaces(str); std::cout << "|" << s << "|" << std::endl; // |Hello World| return 0; } |
That’s all about replacing consecutive whitespace with a single space in 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 :)