How to determine if a string is a number with C++?

后端 未结 30 1918
遇见更好的自我
遇见更好的自我 2020-11-22 08:46

I\'ve had quite a bit of trouble trying to write a function that checks if a string is a number. For a game I am writing I just need to check if a line from the file I am r

相关标签:
30条回答
  • 2020-11-22 09:11

    A solution based on a comment by kbjorklu is:

    bool isNumber(const std::string& s)
    {
       return !s.empty() && s.find_first_not_of("-.0123456789") == std::string::npos;
    }
    

    As with David Rector's answer it is not robust to strings with multiple dots or minus signs, but you can remove those characters to just check for integers.


    However, I am partial to a solution, based on Ben Voigt's solution, using strtod in cstdlib to look decimal values, scientific/engineering notation, hexidecimal notation (C++11), or even INF/INFINITY/NAN (C++11) is:

    bool isNumberC(const std::string& s)
    {
        char* p;
        strtod(s.c_str(), &p);
        return *p == 0;
    }
    
    0 讨论(0)
  • 2020-11-22 09:13

    Using <regex>. This code was tested!

    bool isNumber(const std::string &token)
    {
        return std::regex_match(token, std::regex("(\\+|-)?[0-9]*(\\.?([0-9]+))$"));
    }
    
    0 讨论(0)
  • 2020-11-22 09:13
    bool isNumeric(string s){
        if ( !s.empty() && s[0] != '-' )
            s = "0" + s; //prepend 0
    
        string garbage;
    
        stringstream ss(s); 
        ss >> *(auto_ptr<double>(new double)) >> garbage;
    /*
    //the line above extracts the number into an anonymous variable. it could also be done like this:
    double x;
    ss >> x >> garbage;
    */
        //if there is no garbage return true or else return false
        return garbage.empty(); 
    }
    

    how it works: the stringstream >> overload can convert strings to various arithmetic types it does this by reading characters sequentially from the stringstream (ss in this case) until it runs out of characters OR the next character does not meet the criteria to be stored into the destination variable type.

    example1:

    stringstream ss("11");
    double my_number;
    ss >> my_number; //my number = 11
    

    example2:

    stringstream ss("011");
    double my_number;
    ss >> my_number; //my number = 11
    

    example3:

    stringstream ss("11ABCD");
    double my_number;
    ss >> my_number; //my number = 11 (even though there are letters after the 11)
    

    the "garbage" variable explanation":

    why not just check if extraction into my double has a valid value and then return true if it does?

    notice example3 above will still successfully read the number 11 into the my_number variable even if the input string is "11ABCD" (which is not a number).

    to handle this case we can do another extraction into a string variable(which I named garbage) which can read anything that may have been left over in the string buffer after the initial extraction into the variable of type double. If anything is left over it will be read into "garbage" which means the full string passed in was not a number (it just begins with one). in this which case we'd want to return false;

    the prepended "0" explanation":

    attempting to extract a single character into a double will fail(returning 0 into our double) but will still move the string buffer position to after the character. In this case our garbage read will be empty which would cause the function to incorrectly return true. to get around this I prepended a 0 to the string so that if for example the string passed in was "a" it gets changed to "0a" so that the 0 will be extracted into the double and "a" gets extracted into garbage.

    prepending a 0 will not affect the value of the number so the number will still be correctly extracted into our double variable.

    0 讨论(0)
  • 2020-11-22 09:15

    Why reinvent the wheel? The C standard library (available in C++ as well) has a function that does exactly this:

    char* p;
    long converted = strtol(s, &p, 10);
    if (*p) {
        // conversion failed because the input wasn't a number
    }
    else {
        // use converted
    }
    

    If you want to handle fractions or scientific notation, go with strtod instead (you'll get a double result).

    If you want to allow hexadecimal and octal constants in C/C++ style ("0xABC"), then make the last parameter 0 instead.

    Your function then can be written as

    bool isParam(string line)
    {
        char* p;
        strtol(line.c_str(), &p, 10);
        return *p == 0;
    }
    
    0 讨论(0)
  • 2020-11-22 09:16

    Try this:

    isNumber(const std::string &str) {    
      return !str.empty() && str.find_first_not_of("0123456789") == string::npos;
    }
    
    0 讨论(0)
  • 2020-11-22 09:19

    My solution using C++11 regex (#include <regex>), it can be used for more precise check, like unsigned int, double etc:

    static const std::regex INT_TYPE("[+-]?[0-9]+");
    static const std::regex UNSIGNED_INT_TYPE("[+]?[0-9]+");
    static const std::regex DOUBLE_TYPE("[+-]?[0-9]+[.]?[0-9]+");
    static const std::regex UNSIGNED_DOUBLE_TYPE("[+]?[0-9]+[.]?[0-9]+");
    
    bool isIntegerType(const std::string& str_)
    {
      return std::regex_match(str_, INT_TYPE);
    }
    
    bool isUnsignedIntegerType(const std::string& str_)
    {
      return std::regex_match(str_, UNSIGNED_INT_TYPE);
    }
    
    bool isDoubleType(const std::string& str_)
    {
      return std::regex_match(str_, DOUBLE_TYPE);
    }
    
    bool isUnsignedDoubleType(const std::string& str_)
    {
      return std::regex_match(str_, UNSIGNED_DOUBLE_TYPE);
    }
    

    You can find this code at http://ideone.com/lyDtfi, this can be easily modified to meet the requirements.

    0 讨论(0)
提交回复
热议问题