Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > the easy way to check if the string is a number or not?

Reply
Thread Tools

the easy way to check if the string is a number or not?

 
 
kathy
Guest
Posts: n/a
 
      03-08-2006
I want to know what is the easy way to check if a string is a number or
not? the number can be int, float, double, scientific,...

what is the easy way for only interger?

 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-08-2006
kathy wrote:
> I want to know what is the easy way to check if a string is a number or
> not? the number can be int, float, double, scientific,...


Try to read it using a 'std::istringstream'. If this is not what you
want, check out Boost's regular expression library and provide a
regular expression describing the formats you want.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      03-08-2006
On 8 Mar 2006 13:34:46 -0800, "kathy" <(E-Mail Removed)> wrote:

>I want to know what is the easy way to check if a string is a number or
>not? the number can be int, float, double, scientific,...


check out strtod:
http://www.opengroup.org/onlinepubs/...ns/strtod.html

>what is the easy way for only interger?


strtol

In both cases you need to understand what the 'endptr' does. Hint:
strip trailing whitespace before calling strtod or strtol. Encapsulate
the check in a reusable function.

Best wishes,
Roland Pibinger
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      03-09-2006

kathy wrote in message
<(E-Mail Removed). com>...
>I want to know what is the easy way to check if a string is a number or
>not? the number can be int, float, double, scientific,...
>what is the easy way for only interger?


If the others did not give you the answer you wanted, maybe this will help
you.

// ----------------------------------------------
// #include <ccytpe> // ISO C++ 14882: std::isdigit()
std::cout<<"\n_______ isdigit test _______"<<std::endl;
int Inum(0);
double Dnum(0);
std::string TestForNum("Hi 4 76.5num 42.7e-5");
std::cout<<"Test string = "<<TestForNum<<"."<<std::endl;
for(size_t i(0); i < TestForNum.size(); ++i){
std::cout<<"char at "<<i<<" is "<<TestForNum.at(i)<<", a ";
if( std::isdigit( TestForNum.at(i) ) ){
std::istringstream strStream( TestForNum.substr(i) );
strStream >> Inum;
strStream.clear();
strStream.str( TestForNum.substr(i) );
strStream >> Dnum;
strStream.clear();
std::cout<<"digit Inum="<<Inum
<<" Dnum="<<Dnum<<std::endl;
}
else{ std::cout<<"alpha"<<std::endl;}
}
std::cout<<"_______ isdigit test _______Done"<<std::endl;
/* -- output --
_______ isdigit test _______
Test string = Hi 4 76.5num 42.7e-5.
char at 0 is H, a alpha
char at 1 is i, a alpha
char at 2 is , a alpha
char at 3 is 4, a digit Inum=4 Dnum=4
char at 4 is , a alpha
char at 5 is 7, a digit Inum=76 Dnum=76.5
char at 6 is 6, a digit Inum=6 Dnum=6.5
char at 7 is ., a alpha
char at 8 is 5, a digit Inum=5 Dnum=5
char at 9 is n, a alpha
char at 10 is u, a alpha
char at 11 is m, a alpha
char at 12 is , a alpha
char at 13 is 4, a digit Inum=42 Dnum=0.000427
char at 14 is 2, a digit Inum=2 Dnum=2.7e-005
char at 15 is ., a alpha
char at 16 is 7, a digit Inum=7 Dnum=7e-005
char at 17 is e, a alpha
char at 18 is -, a alpha
char at 19 is 5, a digit Inum=5 Dnum=5
_______ isdigit test _______Done
*/
// ----------------------------------------------

Open <ccytpe> in an editor for a list of more useful items.

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if String.matches() AND (if yes) extract number from String? Jochen Brenzlinger Java 5 11-21-2011 07:43 PM
Easy way to check 2 similar cameras please Rob G Digital Photography 10 05-30-2009 11:30 PM
whats an easy way to enable spell check? twfsa Computer Information 3 02-05-2006 05:06 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM
Easy way to check modules for python version compatibility? Stephen Toledo-Brown Python 1 03-04-2005 07:52 PM



Advertisments