filter a string?

how in c++ am i able to filter a string? my small app allows someone to enter in some text, im looking to filter out unneeded letters/characters, characters such as '\,<".< and so on, what is the best way to do this?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
std::string Filter(const std::string &to, const std::string &remove)
{
    std::string final;
    for(std::string::const_iterator it = to.begin(); it != to.end(); ++it)
    {
        if(remove.find(*it) == std::string::npos)
        {
            final += *it;
        }
    }
    return final;
}
Demo: http://ideone.com/BOOSX
Last edited on
That wouldn't work for chars...
It works. See the demo.
apart from adding the characters / symbols i want to remove (theres alot including foreign characters), how would i make it just accept "0-9 a-z A-Z _" and any other characters are removed? whats the best and fastest method for doing so?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
std::string Filter(const std::string &to) //EDIT: removed const std::string &remove
{
    std::string final;
    for(std::string::const_iterator it = to.begin(); it != to.end(); ++it)
    {
        if((*it >= '0' && *it <= '9') || (*it >= 'a' && *it <= 'z') || (*it >= 'A' && *it <= 'Z') || *it == '_')
        {
            final += *it;
        }
    }
    return final;
}
Last edited on
final is of type std::string which does not work for strcpy. Also, how can stringupdated know how big to be? Just return final from the function. I updated my post.
Last edited on
Topic archived. No new replies allowed.