I need help with a function that cleans char

the function cleans the any extracharacters before the word but i dont know what to do for characters after the word.

so if you know what to do in second while loop let me know!






std::string clean(std::string inputstr)
{
std::string extrachar = "!@#$%^&*()_-+={}[]:;\"\'`<>,.?/|\\";

while(inputstr.find_first_of(extrachar) == 0)
{
inputstr=inputstr.substr(1);
}

while(inputstr.find_last_of(extrachar)== 0)
{



}

Maybe:
1
2
3
4
    while (inputstr.find_last_of(extrachar)!=string::npos)
    {
        inputstr.pop_back();
    }


But why all the looping?
1
2
3
4
5
6
7
8
9
10
11
12
std::string clean(std::string inputstr)
{
    std::string extrachar = "!@#$%^&*()_-+={}[]:;\"\'`<>,.?/|\\";
    
    size_t start  = inputstr.find_first_not_of(extrachar);
    size_t finish = inputstr.find_last_not_of(extrachar);
    
    if (start != string::npos)
        inputstr = inputstr.substr(start, finish-start+1);
        
    return inputstr;
}
Topic archived. No new replies allowed.