Locating chars in strings etc.
-
It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.
-
It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.
if (strchr (blahblah, char)) eraseit ; Is this what you are looking for?
-
It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.
CreepingFeature wrote: ...I got myself a string... Would that be an STL string or an MFC CString?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
if (strchr (blahblah, char)) eraseit ; Is this what you are looking for?
Thanks!!! ;)