Is there an easy way to take out certain characters in a string, or do I have to do the old way and implement a for loop that removes all occurences of it?
Printable View
Is there an easy way to take out certain characters in a string, or do I have to do the old way and implement a for loop that removes all occurences of it?
strchr should do the job look it up in MSDN
If you are using a C++ string, there are several ways. The easiest might be using the erase-remove idiom:Code:str.erase(std::remove(str.begin(), str.end(), 'x'), str.end());
Damn I forgot you'd be using that. Sorry.
You could also use remove with C style strings (I think this is safe):Code:*(std::remove(&str[0], &str[0] + strlen(str), 'x')) = '\0';
Ah, thanks guys I've been hassling over this for hours.
Nope, I'm using char*s. I do like the ease of string better, but nothing really uses strings, so I just keep it char*s.Quote:
Damn I forgot you'd be using that. Sorry.
what do you mean nothing really uses strings,
i can't think of one thing i would choose
to use character arrays over strings.
Some things, like
require C-strings...but strings are so much more manageable and safe, and if you want to do that sort of thing, all you have to do is use .c_str()...Code:ofstream fname("filename.txt");
Code:std::string fileString = "filename.txt";
ofstream fname( fileString.c_str() );
Only problem is that c_str() returns a const
Why do you find 'const char*' problematic?Quote:
Originally Posted by Ganoosh
Assigning it to a non-const char*, which most of mine are.Quote:
Originally Posted by Zach L.
Why would you have a non-const char* if you're using std::strings? Does not compute!
You can use std::copy to copy characters to your non-const char buffer.
Right. Rashakil Fol pretty much beat me to my point: std::string is a big class (perhaps too big, even), with lots of functionality, which is enhanced by the algorithms of STL, so essentially, anything you'd need a char* as opposed to a const char* (which really ought to be limited to libraries and such that already have the dependeny on C-style strings), you would do with algorithms and std::string's built in functionality.
So, as Rashakil said, "Does not compute!"
Cheers
cin.ignore may work
if only I could slap you...Quote:
Originally Posted by Renegade
if you're going to stick with char* (which there's NOTHING wrong with), you may want to look up strstr, or if you're only looking for characters, strchr. that will only get you the position of the character. the only way to get rid of it is to overwrite it or advance the rest of the string over it...
like others have said, if you don't NEED char*, you may want to look into an std::string, because algorithms like this have already been created and optimized for you.