Thread: single-string to char

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    single-string to char

    I see a lot of functions are meant for char's and not strings. FOr example, isalpha().

    In my case I want to use isalpha but I have a string. The good news is it's a single digit string using substr to grab each individually.

    I want to cast my (val.substr(1,1) into a char for isalpha to process. Everythign I read says string to char is a no-no.

    Ideas?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Why are you using substr to grab 1 character? Why not operator[]? And why don't you just pass that value to std::isalpha?

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    operator? signs and symbols? Got a good link?

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    What? operator[] is the [] operator. as in std::string rtfm("foo"); rtfm[2]; evaluates to an object of type 'char' having a value of the second index in the rtfm object.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    i thought that you meant [] but I didn't know if there was something I was missing...I didn't realize I could use it to pull individual characters from a string. Very nice. thanks.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    That's what it's there for.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The SGI and Dinkumware sites have good documentation on standard library classes like string, and your favorite book should as well.

    There are no isalpha type methods for strings because it is usually pretty easy to combine a character based method like isalpha with a string using an algorithm or a simple loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM