What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)
Code:std::string sPrimaryNumber = "98";
sPrimaryNumber.find_first_not_of("0123456789");
Printable View
What are some ways to determine if a std::string contains all numeric data? (Alpha representations of numeric data)
Code:std::string sPrimaryNumber = "98";
sPrimaryNumber.find_first_not_of("0123456789");
That would do it....what else are you asking for?
There may be other calls you could make, but the basic algorithm isn't going to be any different in most cases. The complexity of that algorithm is going to be O(n), and you can't really do better than that.
Code:bool check_string_numeric(std::string str)
{
for(int i = 0; i < str.length(); i++)
{
if(!( (str.c_str()[i] >= '0') && (str.c_str()[i] <= '9') ) )
{
//Not numeric
return false;
}
}
//numeric
return true;
}
Hi, folks
I'm new to this forum... sorry for barging in here, but I cannot seem to figure out how to start a thread on this forum! Can you please give me a clue!
Best regards and please forgive the intrusion ...
DKean
Regards,Code:#include <algorithm>
#include <string>
#include <cctype>
bool IsNotDigit(char c)
{
return !isdigit(c);
}
bool check_string_numeric(const std::string& str)
{
return (std::find_if(str.begin(), str.end(), IsNotDigit) == str.end());
}
Paul McKenzie
Easier to just use isdigit directly:If you've got access to the Boost SAL, this ugliness then becomes:Code:bool check_string_numeric(const std::string& str)
{
return (std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun(::isdigit))) == str.end());
}
Code:boost::all(str, boost::is_digit());
I require an algorithm to do exactly this and the one I wrote hasn't failed me yet.
This accepts any numeric value: decimal, hex, scientific notation, and has special recognition for Infinity and -Infinity.
Code:char NumberChars[] = {"0123456789"};
char HexChars[] = {"012345679ABCDEFabcdef"};
string LS_INFINITY = "Infinity";
string NEG_LS_INFINITY = "-Infinity";
long double Infinity = 1.0f/0.0f;
bool IsNumber(const string & str, long double & out){
if (str == LS_INFINITY){
out = Infinity;
return true;
} else if (str == NEG_LS_INFINITY){
out = -Infinity;
return true;
}
if (str[0] == '-'){
if ((str[1] == '0') && (str[2] == 'x')){
if (str.find_first_not_of(HexChars, 3) != string::npos) return false;
long long tout;
stringstream ss;
ss.str(str);
ss << setbase(16);
ss >> tout;
out = (long double) tout;
return true;
} else { //negative, non hex, keep looking for failure
size_t pp = str.find_first_not_of(NumberChars, 1);
if (pp != string::npos){
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
} else if (str[pp] == '.'){
pp = str.find_first_not_of(NumberChars, pp + 1);
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars) != string::npos) return false;
}
} else {
return false;
}
}
}
} else if ((str[0] == '0') && (str[1] == 'x')){
if (str.find_first_not_of(HexChars, 2) != string::npos) return false;
long long tout;
stringstream ss;
ss.str(str);
ss << setbase(16);
ss >> tout;
out = (long double) tout;
return true;
} else {
size_t pp = str.find_first_not_of(NumberChars);
if (pp != string::npos){
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars, pp + 2) != string::npos) return false;
} else if (str[pp] == '.'){
pp = str.find_first_not_of(NumberChars, pp + 1);
if ((str[pp] == 'e') && ((str[pp + 1] == '+') || (str[pp + 1] == '-'))){ //negative scientifi notation
if (str.find_first_not_of(NumberChars) != string::npos) return false;
}
} else {
return false;
}
}
}
out = atof(str.c_str());
return true;
}
Here is a call of your function that returns an incorrect result:Quote:
Originally Posted by ninja9578
If you had written a helper function for your otherwise repeated hexadecimal conversion code you might have better avoided this mistake.Code:long double x;
bool result = IsNumber("0xg", x);
But not octal, apparently :)Quote:
Originally Posted by ninja9578
Out of curiosity, but why do you write:
instead of using std::numeric_limits<long double>::infinity() or some equivalent existing constant?Code:long double Infinity = 1.0f/0.0f;
Oh, and of course you should declare those constants as const.
Oops, fixed now :) 0xg will now fail as it should. I have no need for Octal because my scripting language doesn't support it
Some other strings to consider:
"0X1"
"1L"
"1u"
"0x"
"+1"
Thanks everyone