Sign in to follow this  
Tradone

If the string is all numbers

Recommended Posts

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[i] < 48 || str[i] > 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[i] < (_E) '0' || Str[i] > (_E) '9')
{
return false;
}
}

return true;
}

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