Remove consecutive whitespace
When receiving input from users, it's common to end up with excessive consecutive whitespace characters in your strings. This recipe presents a function for removing consecutive spaces, even when it includes tabs or other whitespace characters.
How to do it…
This function leverages the std::unique()
algorithm to remove consecutive whitespace characters from a string.
- In the
bw
namespace, we start with a function to detect whitespace:template<typename T> bool isws(const T& c) { constexpr const T whitespace[]{ " \t\r\n\v\f" }; for(const T& wsc : whitespace) { if(c == wsc) return true; } return false; }
This templated isws()
function should work with any character type.
- The
delws()
function usesstd::unique()
to erase consecutive...