locked
Equivalents of C++ isalpha() and isdigit() etc. in C#

    Question

  • I am converting some C++ code where isalpha() and isdigit() are used.

    Isalpha(char) returns true if the character is a letter of the alphabet.

    Isdigit(char) returns true is the character is numeric.

    I am parsing a line in a file and need to know when the next substring or character I encounter is a number or a letter so I can tell what kind of data is in the substring.  It also would help me to know what type of conversion/parsing is appropriate to use.

    Any help would be appreciated.

    Marc

    Friday, August 27, 2010 3:29 AM

Answers

  • // isaplha(c) equivalent in C#
    var isAlpha = char.IsLetter('a');
    
    // isdigit(c) equivalent in C#
    var isDigit = char.IsNumber('8');
    
    

    As for parsing an entire string, perhaps a regular expression would help.

     

    Cheers,

    -chris


    Mark the best replies as Answers! | Blog: http://devpinoy.org/blogs/cvega
    • Marked as answer by Marc David Friday, August 27, 2010 6:36 PM
    Friday, August 27, 2010 3:41 AM
  • char.IsNumber will work, but char.IsDigit will produce identical results to the C++ isdigit.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by Marc David Friday, August 27, 2010 6:36 PM
    Friday, August 27, 2010 5:01 AM

All replies

  • // isaplha(c) equivalent in C#
    var isAlpha = char.IsLetter('a');
    
    // isdigit(c) equivalent in C#
    var isDigit = char.IsNumber('8');
    
    

    As for parsing an entire string, perhaps a regular expression would help.

     

    Cheers,

    -chris


    Mark the best replies as Answers! | Blog: http://devpinoy.org/blogs/cvega
    • Marked as answer by Marc David Friday, August 27, 2010 6:36 PM
    Friday, August 27, 2010 3:41 AM
  • char.IsNumber will work, but char.IsDigit will produce identical results to the C++ isdigit.


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by Marc David Friday, August 27, 2010 6:36 PM
    Friday, August 27, 2010 5:01 AM
  • Thanks for the answers.

    I sent in the question late at night.  Soon after, I found the answer, but it already was past my bedtime.

    That happens some times: finding the answer not long after I have gotten to the point of giving up.  Actually, an answer in the VB forums pointed me in the right direction.

    Thanks again.

    Marc

     

    Friday, August 27, 2010 6:39 PM