Original Post
I was wondering if anyone could shed some light on a problem I'm having.
I have a string that I am removing unwanted characters from. The characters I want removed are:
(
)
:
,
space
The first four characters are no problem, but any conditional statements based upon the space do not fire when tested at the same time as the first four.
E.g. the following doesn't work. All characters apart from space are removed. If I test only for the space, then spaces are removed.
I've tried case statements, too. Same behaviour (understandable). However, if I seperate the space condition from the if statement and put its own case statement, it works. Likewise if I seperate it into its own if statement from a main case statement it works. Seperating into the same kind of statement does not work.
I was originally using string::rfind() and searching for each character to be removed in turn and that worked. I don't know why I started playing around with case/if statements, but this behaviour has been bothering me for a while and I refuse to go back to rfind() without understanding what is wrong here.
I have a string that I am removing unwanted characters from. The characters I want removed are:
(
)
:
,
space
The first four characters are no problem, but any conditional statements based upon the space do not fire when tested at the same time as the first four.
E.g. the following doesn't work. All characters apart from space are removed. If I test only for the space, then spaces are removed.
string removeCharacters(string modified)
{
int tempSize = modified.size();
for (int it = ( tempSize-1); it > 0; it-- )
{
if ( ( modified[it] == ' ' ) || ( modified[it] == '(') ) || ( modified[it] == ')' ) || ( modified[it] == ':' ) || ( modified[it] == ',' ) )
{
modified.erase(it, 1);
it--;
}
}
return modified;
}
I've tried case statements, too. Same behaviour (understandable). However, if I seperate the space condition from the if statement and put its own case statement, it works. Likewise if I seperate it into its own if statement from a main case statement it works. Seperating into the same kind of statement does not work.
I was originally using string::rfind() and searching for each character to be removed in turn and that worked. I don't know why I started playing around with case/if statements, but this behaviour has been bothering me for a while and I refuse to go back to rfind() without understanding what is wrong here.