user14046709
user14046709

Reputation:

Delete tabs as well as spaces in c++

I wrote the following c++ code which removes spaces from beginning and end of a string, But the problem with it is that it doesn't remove tabs, how may I fix that?

Plus, is there anything similar to tabs and spaces? (I am reading lines from file)

string trim_edges(string command) {
    const auto command_begin = command.find_first_not_of(' ');
    if (command_begin == std::string::npos)
        return "";

    const auto strEnd = command.find_last_not_of(' ');
    const auto strRange = strEnd - command_begin + 1;

    return command.substr(command_begin, strRange);
}

Upvotes: 1

Views: 808

Answers (1)

Botje
Botje

Reputation: 30830

find_first_not_of and find_last_not_of can also take a string as a set of characters to skip:

const auto command_begin = command.find_first_not_of(" \t");
const auto strEnd = command.find_last_not_of(" \t");

Upvotes: 7

Related Questions