If the string is all numbers

Started by
3 comments, last by Zakwayda 18 years ago
Is there a standard way to tell if a string consists of all numbers or not?
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.
Mike Popoloski | Journal | SlimDX
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]
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;}
I think this is an errant post; the question was already asked and answered here.

This topic is closed to new replies.

Advertisement