filter a string?

Mar 6, 2012 at 1:57am
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 Mar 6, 2012 at 1:57am
Mar 6, 2012 at 2:01am
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 Mar 6, 2012 at 2:08am
Mar 6, 2012 at 2:12am
That wouldn't work for chars...
Mar 6, 2012 at 2:12am
It works. See the demo.
Mar 6, 2012 at 2:21am
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 Mar 6, 2012 at 2:25am
Mar 6, 2012 at 2:39am
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 Mar 6, 2012 at 3:07am
Mar 6, 2012 at 3:08am
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 Mar 6, 2012 at 3:08am
Topic archived. No new replies allowed.