Easy way to ignore chars?

This is a discussion on Easy way to ignore chars? within the C++ Programming forums, part of the General Programming Boards category; Is there an easy way to take out certain characters in a string, or do I have to do the ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Easy way to ignore chars?

    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

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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());

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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';

  6. #6
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Ah, thanks guys I've been hassling over this for hours.
    Damn I forgot you'd be using that. Sorry.
    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.
    Using Dev-C++ on Windows

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    what do you mean nothing really uses strings,
    i can't think of one thing i would choose
    to use character arrays over strings.

  8. #8
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Some things, like
    Code:
    ofstream fname("filename.txt");
    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:
    std::string fileString = "filename.txt";
    ofstream fname( fileString.c_str() );

  9. #9
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Only problem is that c_str() returns a const
    Using Dev-C++ on Windows

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Ganoosh
    Only problem is that c_str() returns a const
    Why do you find 'const char*' problematic?
    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.

  11. #11
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Quote Originally Posted by Zach L.
    Why do you find 'const char*' problematic?
    Assigning it to a non-const char*, which most of mine are.
    Using Dev-C++ on Windows

  12. #12
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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.

  13. #13
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.

  14. #14
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    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 =)

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Renegade
    cin.ignore may work
    if only I could slap you...

    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy String position question...
    By Striph in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 09:48 PM
  2. question about ignore()????
    By newbie02 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2003, 09:27 AM
  3. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 02:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21