473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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?

Mar 8 '06 #1
3 9872
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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #2
On 8 Mar 2006 13:34:46 -0800, "kathy" <yq*****@yahoo.com> 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
Mar 8 '06 #3

kathy wrote in message
<11*********************@u72g2000cwu.googlegroups. 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
Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
4
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
3
by: Water Cooler v2 | last post by:
What is the .NET equivalant of the VB function String(Number as Long, Character)? I am forgetting it at the moment.
4
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are...
2
by: olskar | last post by:
How can I check the number of values in an array and echo them in my PHP script?
4
by: JBiggsCC | last post by:
I have a very simple login page which takes an ID number via a HTML form GET. What is easiest way to check that ID number against an Access DB to see if it exists? I want to redirect with the...
4
by: Aaryan123 | last post by:
Function to check a number between a range in perl ========================================= Back ground: This situation has been fixed in Perl5.005. Use of .. in a for loop will iterate over...
5
by: Cylix | last post by:
Is there any existing method in VB.NET or any 3-third party function can find out the language in a string? Let say, isChinese? isFrench?
11
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
1
by: Raman Pahwa | last post by:
plz tell me how to check the number of open connections in ASP code.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.