Jump to content
  • Advertisement
Sign in to follow this  
Tradone

If the string is all numbers

This topic is 4437 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
Quote:
Is there a standard way to tell if a string consists of all numbers or not?


Here is how I would do it.


bool IsStringNumeric( std::string str )
{
for( UINT i = 0; i < str.size(); i++ )
{
if( str < 48 || str > 57 )
return false;
}

return true;
}



Then again, I'm sure there is some easier way I'm not thinking off.
Disclaimer: This may not work, I just came up with it.

Share this post


Link to post
Share on other sites
You could just go through the stream and check -
for ( std::string::iterator itor = myString.begin(); itor != myString.end(); itor++ )
if ( *itor < '0' || *itor > '9' ) return false;
return true;


There may also be a way to do it using stringstreams.

EDIT: oops, beaten to it [wink]

Share this post


Link to post
Share on other sites
Didn't you already post a topic like this????
Anyways this is untested code:


template <typename _E>
bool is_all_numbers(const std::basic_string<_E>& Str)
{
for (size_t i = 0;i < Str.length();i++)
{
if (Str < (_E) '0' || Str > (_E) '9')
{
return false;
}
}

return true;
}

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!