Remove Characters from a string - C++ STL

C++ examples for STL:string

Description

Remove Characters from a string

Demo Code

#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    const std::string STR1 = "by";
    const std::string STR2 = "BY";

    std::cout << "Enter a string: ";

    std::string base;/*from  w w  w.  j av a2 s .c  o  m*/
    std::getline(std::cin, base);

    size_t pos = base.find(STR1, 0);

    while (pos != std::string::npos) {
        base.erase(pos, 2);
        pos = base.find(STR1, pos + 1);
    }

    pos = base.find(STR2, 0);

    while (pos != std::string::npos) {
        base.erase(pos, 2);
        pos = base.find(STR2, pos + 1);
    }

    std::cout << base << std::endl;

    return 0;
}

Result


Related Tutorials