Sign in to follow this  
Tradone

If the string is all numbers

Recommended Posts

kmccusker    196
If you are using the .NET framework, there is a Char.IsDigit() method available. Otherwise you could convert characters in a string to their ASCII value and then check to see if the string contains the 0-9 number's ASCII values.

I am guessing there are more ways to do this.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by Tradone
Is there a standard way to tell if a string consists of all numbers or not?
If this is C++, a standard-ish method would be:
if (boost::algorithm::all(myString, boost::algorithm::is_digit()) {
// ...
}
I don't have the reference in front of me so I might not have gotten the syntax exactly right, but it should be something close to that.

Share this post


Link to post
Share on other sites
BeerNutts    4401
This handles both Hex or Decimal numbers.


BOOL IsNumber(char *cString, BOOL bIsHex)
{
BOOL bIsDigit = TRUE;

while((*cString != 0) && bIsDigit)
{
if (bIsHex)
{
bIsDigit = isxdigit(*cString);
}
else
{
bIsDigit = isdigit(*cString);
}
cString++;
}

return bIsDigit;
}




(my rating will now go down for suggesting a "C" solution and not a c++ solution)

[Edited by - BeerNutts on April 24, 2006 3:02:13 PM]

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by BeerNutts
This handles both Hex or Decimal numbers.

*** Source Snippet Removed ***
if (boost::algorithm::all(myString, boost::algorithm::is_xdigit()) {
// ...
}
:-)

Share this post


Link to post
Share on other sites
Zahlman    1682
And if for some silly reason you don't like Boost :)


// We need this wrapper to provide typedefs used by not1.
struct isDigit {
typedef char argument_type;
typedef bool result_type;

result_type operator() (argument_type x) {
return std::is_digit(x);
}
};

if (std::find_if(myString.begin(), myString.end(), std::not1(isDigit())) ==
myString.end()) {
// We didn't find a non-digit, so they are all digits.
}

Share this post


Link to post
Share on other sites
Tradone    100
Quote:
Original post by jyk
Quote:
Original post by BeerNutts
This handles both Hex or Decimal numbers.

*** Source Snippet Removed ***
if (boost::algorithm::all(myString, boost::algorithm::is_xdigit()) {
// ...
}
:-)


Just what I was looking.
Everybody thanks for the contributions!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.


You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too). If you want to be able to spot all the different kind of numbers, I'm afraid you'll have to use a regular expression.

Regards,

Share this post


Link to post
Share on other sites
rdansie    114
bool isStringNumber(char *str, int len){
bool returnResult = TRUE;

if((str[0] > '0') && (str[0] < '9')){
if(len > 0)
returnResult = isStringNumber(&(char[1]), len-1);
}else
returnResult = FALSE;


return returnResult;
}

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by rdansie
bool isStringNumber(char *str, int len){
bool returnResult = TRUE;

if((str[0] > '0') && (str[0] < '9')){
if(len > 0)
returnResult = isStringNumber(&(char[1]), len-1);
}else
returnResult = FALSE;


return returnResult;
}
If you're specifically offering a C solution, then great, I suppose (although your function has a couple of errors in it and really doesn't need to be recursive).

However (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.

Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library. If you're seriously proposing the above or something like it as a solution, I think you need to provide some supporting arguments as to why it's superior to what's already been suggested.

Anyway, I don't mean to be negative; really my motivation is just to clarify to those who might read this thread (and might not have the experience to sort the wheat from the chaff, as it were) that standardized solutions are generally to be preferred.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by jyk
However (and this is editorializing a bit) I don't quite understand why people insist on posting hand-rolled, untested, unsafe, and/or non-standard solutions to threads like this after the (arguably) correct solution has already been given.

Read the thread. The test can be performed in one line using boost, and if you don't like that Zahlman posted a nice alternative using functors and the standard library.


Well, it *is* good to know the (written very specific to the problem) algorithms in case you ever find yourself in an environment (language, target device, etc. etc.) that doesn't provide support for what you want to do. But then, only really experienced programmers ought to be working in those environments - people who are capable of coming up with these things themselves, and getting them right (able to give a proof, and also have it pass tests) the first time (or at least within the first few times).

Sadly, the world doesn't always work that way ;(

Share this post


Link to post
Share on other sites
kSquared    1356
Quote:
Original post by Emmanuel Deloget
Quote:
Original post by Anonymous Poster
Also keep in mind that if a string that has "-123.567" is a number you will need to check if "-" and "." are allowed. I do not know if you are interested in the punctuation of a number, but I will leave that up to you.


You have a point here. Of course, testing if a string represents any kind of number can be tricky ("+-+123.344e008" is a number too).

That's technically an expression, not a numeric literal. (But the result of the expression is the literal "-123.344e008".)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The topic is called "if the string is all numbers", so I assume that there are really only digits in the string, no sign, radix character or any other non-digit character.

But if they're allowed there is a really easy and lame way:

double strAsDouble = atof( theString );
if( strAsDouble != 0.0 ) //a double can store a natural number without loss of precision, so comparison is possible



The only thing I'm wondering is that atof is returning 0.0, which can also be a valid result of converting a string to a number (according to http://www.cplusplus.com/ref/cstdlib/atof.html). That's really strange, that in the standard, there is a function with such errorneous error handling.

Share this post


Link to post
Share on other sites
Roboguy    794
Quote:
Original post by Tradone
What is the header file for `boost::algorithm'?
Sorry, I know this is a dumb question, but I'm not getting any matches..


You need Boost to use Boost::algorithm.

Share this post


Link to post
Share on other sites
Tradone    100
Yea, I have the boost library installed.
I'm using their serialization class to serialize my map files, and also use their lexical cast :D

Do you know the header files that I need to include for boost::algorithms::all isdigit() ??

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this