Jump to content
  • Advertisement
Sign in to follow this  
Tradone

If the string is all numbers

This topic is 4436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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
Quote:
Original post by Tradone
Is there a standard way to tell if a string consists of all numbers or not?

What language are we talking about?

Share this post


Link to post
Share on other sites
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!