473,379 Members | 1,184 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,379 software developers and data experts.

any STL/string function available for determing string contains numeric


Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero

Apr 4 '07 #1
14 2890
ni**********@st.com wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
No. You would have to try one of the C library numeric conversion
functions on the string's contents.

--
Ian Collins.
Apr 4 '07 #2
No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me
Ian Collins wrote:
ni**********@st.com wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
No. You would have to try one of the C library numeric conversion
functions on the string's contents.

--
Ian Collins.
Apr 4 '07 #3
ni**********@st.com wrote:
No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me
Ian Collins wrote:
>ni**********@st.com wrote:
>>Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
How about outputting the string to a stringstream and then
trying to read a double? If it reads sucessfully and the
stream does not have any characters left in it, it is a
number. I'm not sure if this will work for hex numbers
though, but you might try reading an integer as a side-check.

HTH,
- J.
Apr 4 '07 #4
ni**********@st.com wrote:

Please don't top-post.
Ian Collins wrote:
>>ni**********@st.com wrote:
>>>Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero

No. You would have to try one of the C library numeric conversion
functions on the string's contents.
No even that(strtoul, strtod) would not be possible as strtoul ask for
base(16 for hex, 10 for dec)
and my string can containg a non valid format as "0xffff12yzz" or
"0xaa_AMERICA" or "AMERICA" or "0xffff"
I just want to check for the rigth possible numeric value.....
Its content does not matter to me
Then you'll have to parse the string scanning for valid number patterns.

--
Ian Collins.
Apr 4 '07 #5
On Apr 4, 12:22 am, nishit.gu...@st.com wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero

you could try regexp's
http://freshmeat.net/projects/cpp_regex/

bool isValidNumber( string str ) {

if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;

if( num is valid )
return true
else
return false
}

} // end of isValidNumber

you could even return an int or enumeration to determine what type
with further tests
Apr 4 '07 #6
On Apr 4, 6:22 am, nishit.gu...@st.com wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
Boost::regex.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 4 '07 #7
verdverm wrote:
On Apr 4, 12:22 am, nishit.gu...@st.com wrote:
>Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero


you could try regexp's
http://freshmeat.net/projects/cpp_regex/

bool isValidNumber( string str ) {
if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;

if( num is valid )
return true
else
return false
Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.

Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
}

} // end of isValidNumber

you could even return an int or enumeration to determine what type
with further tests
- J.
Apr 4 '07 #8
On Apr 4, 5:40 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
verdverm wrote:
On Apr 4, 12:22 am, nishit.gu...@st.com wrote:
Is their any single fuction available in C++ that can determine that a
string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff" , It can also contain zero
you could try regexp's
http://freshmeat.net/projects/cpp_regex/
bool isValidNumber( string str ) {

if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;
if( num is valid )
return true
else
return false
Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.
The usual formulation in such cases is:

return oss >num >std::ws && oss.get() == EOF ;

If you don't want to allow whitespace, do
oss.unsetf( std::ios::skipws ) ;
before, and drop the "<< std::ws".

The problem here is that there is no one target type which will
accept all possible numbers. If you're willing to use two
streams, however, something like the following should work:

std::ostringstream si( str ) ;
si.unsetf( std::ios::basefield ) ;
std::ostringstream sd( str ) ;
int i ;
double d ;
return (si >i >std::ws && si.get() == EOF)
|| (sd >d >std::ws && sd.get() == EOF) ;

Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
}
} // end of isValidNumber
you could even return an int or enumeration to determine what type
with further tests
With boost::regex, you can capture the substrings which matched
parts of the regular expression in parentheses. Within the
or's, only one should not be empty, and which one tells you
which or matched.

My own regular expression class allows matching several regular
expressions in parallel, with the return code indicating which
one matched:
static Gabi::RegularExpression const
number =
Gabi::RegularExpression( "[+-]?[1-9][0-9]*", 10 ) ;
| Gabi::RegularExpression( "[+-]?0[0-7]*", 8 ) ;
| Gabi::RegularExpression( "[+-]?0[Xx][0-9a-fA-F]+", 16 )
| Gabi::RegularExpression( "[+-]?[0-9]+\.[0-9]*([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?\.[0-9]+([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?[0-9]+[Ee][+-]?[0-9]+",
0 ) ;

std::pair< int, std::string::const_iterator >
result
= number.match( str.begin(), str.end() ) ;
return result.second == str.end()
? result.first
: -1 ;

This would return -1 if no match, 0 for a floating point value,
and the base for an integer value. (I've since added features
to force an error if the match doesn't go to the end of the
string, so you could just use "return number.match(...).first;",
but this version isn't yet present at my site.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 5 '07 #9
Thanx James...1 more query..is regex a part of standard C++???
James Kanze wrote:
On Apr 4, 5:40 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
verdverm wrote:
On Apr 4, 12:22 am, nishit.gu...@st.com wrote:
>Is their any single fuction available in C++ that can determine thata
>string
>contains a numeric value.
>The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
>"0xffff" , It can also contain zero
you could try regexp's
>http://freshmeat.net/projects/cpp_regex/
bool isValidNumber( string str ) {
if(str.length()<2) // do something or the next line crashes
if( str[1] == 'x' ) {
verify proper hex chars
some func to convert to hex
return true
}
else {
ostringstream oss( str )
double num;
num << oss;
if( num is valid )
return true
else
return false
Not really. For the string "123ZZZ" num is valid,
while "123ZZZ" is not a valid number.

The usual formulation in such cases is:

return oss >num >std::ws && oss.get() == EOF ;

If you don't want to allow whitespace, do
oss.unsetf( std::ios::skipws ) ;
before, and drop the "<< std::ws".

The problem here is that there is no one target type which will
accept all possible numbers. If you're willing to use two
streams, however, something like the following should work:

std::ostringstream si( str ) ;
si.unsetf( std::ios::basefield ) ;
std::ostringstream sd( str ) ;
int i ;
double d ;
return (si >i >std::ws && si.get() == EOF)
|| (sd >d >std::ws && sd.get() == EOF) ;

Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.

The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
}
} // end of isValidNumber
you could even return an int or enumeration to determine what type
with further tests

With boost::regex, you can capture the substrings which matched
parts of the regular expression in parentheses. Within the
or's, only one should not be empty, and which one tells you
which or matched.

My own regular expression class allows matching several regular
expressions in parallel, with the return code indicating which
one matched:
static Gabi::RegularExpression const
number =
Gabi::RegularExpression( "[+-]?[1-9][0-9]*", 10 ) ;
| Gabi::RegularExpression( "[+-]?0[0-7]*", 8 ) ;
| Gabi::RegularExpression( "[+-]?0[Xx][0-9a-fA-F]+", 16 )
| Gabi::RegularExpression( "[+-]?[0-9]+\.[0-9]*([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?\.[0-9]+([Ee][+-]?
[0-9]+)?", 0 )
| Gabi::RegularExpression( "[+-]?[0-9]+[Ee][+-]?[0-9]+",
0 ) ;

std::pair< int, std::string::const_iterator >
result
= number.match( str.begin(), str.end() ) ;
return result.second == str.end()
? result.first
: -1 ;

This would return -1 if no match, 0 for a floating point value,
and the base for an integer value. (I've since added features
to force an error if the match doesn't go to the end of the
string, so you could just use "return number.match(...).first;",
but this version isn't yet present at my site.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 5 '07 #10
James Kanze wrote:
>
Personally, I'd go with something like:

static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?
> Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.

The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().

cheers,
- J.
Apr 5 '07 #11
On Apr 5, 2:28 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
James Kanze wrote:
Personally, I'd go with something like:
static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?
Not in C++. On the other hand, you can have suffixes which
specify the type more precisely: U, L or LL for integers, and F
or L for floating point. (Lower case is also accepted, and U
can combine with L or LL.) Thus, 3.14: double, 3.14F: float and
3.14L: long double. I don't know whether the original poster
wanted to accept these or not.

Note that it might be clearer if you specified some of the parts
as constant strings, and build the strings up, rather than
repeating things like "[Ee][+-]?[0-9]+" umpteam times. And
also, that my knowledge of regular expressions was acquired
many, many years ago, and hasn't been updated; I think that
Boost (and some others) accept things like \d for [0-9], \w for
[:space:], etc. (But don't forget that for regex to see the \
in a string literal, you have to double it.) Before using
something like this, I would strongly recommend reading the
documentation for the regular expression package you use.
Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().
Only at the start of conversion. Otherwise, think of the
problems: reading "123" into an int would fail.

The conditions in ios (the base class of istream and ostream)
aren't all that clear: the function good(), for example, does
consider eof(), so something like "if ( stream.good() )" after
an attempted read can fail, even though the read succeeded.
This is one of the reasons why one normally just uses the
conversion to bool (actualy, void*): it tests what you have to
test to know whether the conversion succeeded, and it doesn't
test anything else.

On top of that, the rules for unformatted input are somewhat
different, and the fact that the read "fails" doesn't mean that
it really failed. It only really failed if "! stream &&
stream.gcount() == 0".

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 5 '07 #12
ni**********@st.com wrote:
Thanx James...1 more query..is regex a part of standard C++???
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Apr 5 '07 #13
On Apr 5, 12:08 pm, nishit.gu...@st.com wrote:

[I thought I'd answered this, but since it's not showing
up...]
Thanx James...1 more query..is regex a part of standard C++???
Sort of. It's part of the TR1 (and so already available in some
libraries, without separately installing Boost), and it's been
adopted for C++0x, so it will be fully part of the standard
library in a couple of years.

In the meantime, if your compiler will support it, it's
definitly worth the bother of installing Boost (and installing
Boost is a pain, because they use a very non-standard and very
broken tool for building).

--

Apr 6 '07 #14
James Kanze wrote:
On Apr 5, 2:28 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
>James Kanze wrote:
>>Personally, I'd go with something like:
>> static boost::regex const
number( "^[:space:]*[+-]?"
"(" "([1-9][0-9]*)"
"|" "(0[0-7]*)"
"|" "(0[xX][0-9a-fA-F]+)"
"|" "([0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?)"
"|" "(\.[0-9]+([Ee][+-]?[0-9]+)?)"
"|" "([0-9]+[Ee][+-]?[0-9]+)"
")[:space:]*$" ) ;
return regex_match( str, number ) ;
> That's clever. I _think_ this should be extended with a
"Dd", though -- isn't it a valid exponent delimiter for double
precision values? Like 2.34D+56?

Not in C++.
I must have gotten contaminated by Fortran, even though
I steer away from it :).
On the other hand, you can have suffixes which
specify the type more precisely: U, L or LL for integers, and F
or L for floating point. (Lower case is also accepted, and U
can combine with L or LL.) Thus, 3.14: double, 3.14F: float and
3.14L: long double. I don't know whether the original poster
wanted to accept these or not.
Right.
Note that it might be clearer if you specified some of the parts
as constant strings, and build the strings up, rather than
repeating things like "[Ee][+-]?[0-9]+" umpteam times. And
also, that my knowledge of regular expressions was acquired
many, many years ago, and hasn't been updated; I think that
Boost (and some others) accept things like \d for [0-9], \w for
[:space:], etc. (But don't forget that for regex to see the \
in a string literal, you have to double it.) Before using
something like this, I would strongly recommend reading the
documentation for the regular expression package you use.
OK
>>> Conversely, for the string "123" num may go eof
after reading and hence become invalid, while "123"
is a valid number.
>>The eofbit will be set after reading "123", but this doesn't
mean that the stream has failed. It only means that any further
attempt to read the stream will fail. (Note that std::ws is a
manipulator, which never "fails", i.e. it never sets failbit.)
> Thanks for clarifying that, I was of the impression that
eof() automatically triggers fail().

Only at the start of conversion. Otherwise, think of the
problems: reading "123" into an int would fail.

The conditions in ios (the base class of istream and ostream)
aren't all that clear: the function good(), for example, does
consider eof(), so something like "if ( stream.good() )" after
an attempted read can fail, even though the read succeeded.
This is one of the reasons why one normally just uses the
conversion to bool (actualy, void*): it tests what you have to
test to know whether the conversion succeeded, and it doesn't
test anything else.
Right.
On top of that, the rules for unformatted input are somewhat
different, and the fact that the read "fails" doesn't mean that
it really failed. It only really failed if "! stream &&
stream.gcount() == 0".
Yes.

thanks,
- J.
Apr 6 '07 #15

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

Similar topics

3
by: Bernard Drolet | last post by:
Hi, I have a column containing a string; the string always starts with a letter (a-z), followed by an undefined number of letters, then one number or more. The REGEXP would look like *+ I...
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...
5
by: jupiter | last post by:
Hi guys, I have a problem. I have a list which contains strings and numeric. What I want is to compare them in loop, ignore string and create another list of numeric values. I tried int() and...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
1
by: Ryan Liu | last post by:
Hi, I read a csv file to a datatable. I don't know what are the column types at this moment, so I treat all columns as string type. After I read data from file, I use...
6
by: frohlinger | last post by:
Hi, I need to perform some numeric calculations on a numeric float value, that is received as wstring. I would like to perform a check before converting the wstring to float, checking that indeed...
3
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.