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?
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?
Using Dev-C++ on Windows
strchr should do the job look it up in MSDN
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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.Damn I forgot you'd be using that. Sorry.
Using Dev-C++ on Windows
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
Using Dev-C++ on Windows
Why do you find 'const char*' problematic?Originally Posted by Ganoosh
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
Assigning it to a non-const char*, which most of mine are.Originally Posted by Zach L.
Using Dev-C++ on Windows
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
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
cin.ignore may work
I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)
if only I could slap you...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.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT