C++ – Trim / remove a tab ( “\t” ) from a string


Can anyone suggest a way of stripping tab characters ( "\t"s ) from a string? CString or std::string.

So that "1E10      " for example becomes "1E10".

Best Solution

hackingwords' answer gets you halfway there. But std::remove() from <algorithm> doesn't actually make the string any shorter -- it just returns an iterator saying "the new sequence would end here." You need to call my_string().erase() to do that:

#include <string>
#include <algorithm>    // For std::remove()

my_str.erase(std::remove(my_str.begin(), my_str.end(), '\t'), my_str.end());