how to remove all whitespace from a string in c++ code example
Example 1: c++ remove whitespace from string
#include <algorithm>
int main()
{
std::string str = "H e l l o";
str.erase(remove(str.begin(), str.end(), ' '), str.end());
std::cout << str;
return 0;
}
Example 2: remove space in string c++
string removeSpaces(string str)
{
stringstream s(str);
string temp;
str = "";
while (getline(s, temp, ' ')) {
str = str + temp;
}
return str;
}
Example 3: c++ remove trailing whitespace
remove trailing whitespace