468,512 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Converting a substring to Integer

I have a bunch of strings in a database that have in them a number I
want to extract. Here's the example code I've been working with:

AnsiString tmp = "AMjsdfdsdfj 457756 some description";
int firstDelim = 0;
int secondDelim = 0;

for(int i=0; i<=tmp.Length(); i++)
{
if(IsDelimiter(" ", tmp, i));
{
if ( firstDelim == 0 )
firstDelim = i;
else
{
secondDelim = i;
break;
}
}
if ( secondDelim 0 )
break;
}
int str_length = secondDelim - firstDelim;
ShowMessage(tmp.SubString(firstDelim, str_length));

Can someone give me a hint how to get this to work? Is my code way
off, or is there a better way to do it? it's only about 1500 records
I need to do this on, so speed is not of greatest importance.

Mark
Feb 5 '08 #1
8 4375
On Feb 5, 10:03 am, sparkydarky <markdueck...@gmail.comwrote:
I have a bunch of strings in a database that have in them a number I
want to extract. Here's the example code I've been working with:

AnsiString tmp = "AMjsdfdsdfj 457756 some description";
int firstDelim = 0;
int secondDelim = 0;

for(int i=0; i<=tmp.Length(); i++)
{
if(IsDelimiter(" ", tmp, i));
{
if ( firstDelim == 0 )
firstDelim = i;
else
{
secondDelim = i;
break;
}
}
if ( secondDelim 0 )
break;
}
int str_length = secondDelim - firstDelim;
ShowMessage(tmp.SubString(firstDelim, str_length));

Can someone give me a hint how to get this to work? Is my code way
off, or is there a better way to do it? it's only about 1500 records
I need to do this on, so speed is not of greatest importance.

Mark

use a stringstream
put what you want to convert into the stringstream using operator <<
take what you want to convert out of the stringstream and attempt the
conversion using operator >>
check the fail bit to see if the conversion was successful

std::string text = "123";
int number;
std::stringstream converter;

converter << text;
converter >number;

For the "substring" part of your question, use some creativity and
remember the effect of whitespace in data being inserted and extracted
from an iostream.
Feb 5 '08 #2
You can try this:-

#include <iostream>
#include <sstream>
#include <cctype>

int main()
{
std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";

int j = 0;
std::stringstream cstr;
cstr << s;
int i = 0;
char ch;

while( cstr >ch )
{
if( std::isdigit(ch) )
{
cstr.putback(ch);
break;
}

if( ( j = s.find(" ",j) )== std::string::npos )
{
cstr.seekg(-1);
break;
}

cstr.seekg(++j, std::ios::beg);

}

if(cstr)
{
cstr >i;
}
std::cout << i;

return 0;
}
With Regards,
Reetesh Mukul

sparkydarky wrote:
I have a bunch of strings in a database that have in them a number I
want to extract. Here's the example code I've been working with:

AnsiString tmp = "AMjsdfdsdfj 457756 some description";
int firstDelim = 0;
int secondDelim = 0;

for(int i=0; i<=tmp.Length(); i++)
{
if(IsDelimiter(" ", tmp, i));
{
if ( firstDelim == 0 )
firstDelim = i;
else
{
secondDelim = i;
break;
}
}
if ( secondDelim 0 )
break;
}
int str_length = secondDelim - firstDelim;
ShowMessage(tmp.SubString(firstDelim, str_length));

Can someone give me a hint how to get this to work? Is my code way
off, or is there a better way to do it? it's only about 1500 records
I need to do this on, so speed is not of greatest importance.

Mark
Feb 5 '08 #3
Reetesh Mukul <re***********@gmail.comwrote:
You can try this:-

#include <iostream>
#include <sstream>
#include <cctype>

int main()
{
std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";

int j = 0;
std::stringstream cstr;
cstr << s;
int i = 0;
char ch;

while( cstr >ch )
{
if( std::isdigit(ch) )
{
cstr.putback(ch);
break;
}
From here:
if( ( j = s.find(" ",j) )== std::string::npos )
{
cstr.seekg(-1);
break;
}

cstr.seekg(++j, std::ios::beg);
to here. Is this block of code necessary?
>
}

if(cstr)
{
cstr >i;
}
std::cout << i;

return 0;
}
Wouldn't this be a simpler way of doing the same thing?

int main()
{
std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";

int i = 0;
std::stringstream cstr( s );
char ch = 0;
while( cstr >ch && !isdigit( ch ) )
{ }
cstr.putback( ch );
cstr >i;
cout << i;
}
Feb 6 '08 #4
On Feb 5, 5:03 pm, sparkydarky <markdueck...@gmail.comwrote:
I have a bunch of strings in a database that have in them a
number I want to extract. Here's the example code I've been
working with:
AnsiString tmp = "AMjsdfdsdfj 457756 some description";
int firstDelim = 0;
int secondDelim = 0;
for(int i=0; i<=tmp.Length(); i++)
{
if(IsDelimiter(" ", tmp, i));
{
if ( firstDelim == 0 )
firstDelim = i;
else
{
secondDelim = i;
break;
}
}
if ( secondDelim 0 )
break;
}
int str_length = secondDelim - firstDelim;
ShowMessage(tmp.SubString(firstDelim, str_length));
Can someone give me a hint how to get this to work?
Not unless you give us a hint about the format of the string,
and what determines where you want to look to find the number.
Is my code way off, or is there a better way to do it?
Probably. Most of the time, for this sort of thing,
boost::regex is the simplest solution. Of course, once you've
got the numeric field, you'll have to use std::istringstream to
convert it.

--
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
Feb 6 '08 #5
On Feb 6, 6:35 am, "Daniel T." <danie...@earthlink.netwrote:
Reetesh Mukul <reetesh.mu...@gmail.comwrote:
You can try this:-
#include <iostream>
#include <sstream>
#include <cctype>
int main()
{
std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";
int j = 0;
std::stringstream cstr;
cstr << s;
int i = 0;
char ch;
while( cstr >ch )
{
if( std::isdigit(ch) )
{
cstr.putback(ch);
break;
}

From here:
if( ( j = s.find(" ",j) )== std::string::npos )
{
cstr.seekg(-1);
break;
}
cstr.seekg(++j, std::ios::beg);

to here. Is this block of code necessary?
}
if(cstr)
{
cstr >i;
}
std::cout << i;
return 0;
}

Wouldn't this be a simpler way of doing the same thing?

int main()
{
std::string s = "dsdhjsahdk dsdjdsaj 36782367 sdjdhak";

int i = 0;
std::stringstream cstr( s );
char ch = 0;
while( cstr >ch && !isdigit( ch ) )
{ }
cstr.putback( ch );
cstr >i;
cout << i;

}
Yes, indeed.
Feb 6 '08 #6
On 2008-02-05 11:03:18 -0500, sparkydarky <ma**********@gmail.comsaid:
I have a bunch of strings in a database that have in them a number I
want to extract.
Why not try to solve it at the source? At the database.

If Oracle, try

select to_number(
replace(
translate(COL,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ ',
rpad('#',26+26+1,'#'))
, '#')) as COL
from TABLE;
[...]
--

// kira

Feb 8 '08 #7
James Kanze <ja*********@gmail.comwrote:
Reetesh Mukul <reetesh.mu...@gmail.comwrote:
"Daniel T." <danie...@earthlink.netwrote:
The question I asked Reetesh was why he had such a complex
solution when the above solution, which is far simpler, does
the exact same thing. I'm guessing that it was because he
didn't know about the 'ignore()' member-function, and didn't
know that the putback and op>would do the "right thing" if
the stream failed.
Yes, you are right Daniel. I indeed didn't knew about 'ignore()'
and putback,op's mechanism after things fail. Now an obvious
question is popping in my mind, will it not be good to have
predicates( functors ) in ignore function (or similar functions) ?

Probably because that's not its role. But it's an interesting
suggestion---it should be possible to define a manipulator which
skips using a predicate.
template < typename Pred >
istream& ignore( istream& s, Pred pred )
{
char c = 0;
while ( s.get( c ) && pred( c ) )
{ }
}
More useful, probably, would be a manipulator which skips using a
regular expression.
Using a state-full predicate in the above would probably work for that.
Feb 8 '08 #8
On Feb 8, 12:25 pm, "Daniel T." <danie...@earthlink.netwrote:
James Kanze <james.ka...@gmail.comwrote:
[...]
Probably because that's not its role. But it's an interesting
suggestion---it should be possible to define a manipulator which
skips using a predicate.
template < typename Pred >
istream& ignore( istream& s, Pred pred )
{
char c = 0;
while ( s.get( c ) && pred( c ) )
{ }
}
Which will extract one character too many. And you need either
something like:

template< typename Pred std::istream& ignore( istream& s ) ;

for the interface, or the function should return some special
type, e.g.:

template< typename Pred >
Ignorer< Pred >
ignore( Pred p )
{
return Ignorer< Pred >( p ) ;
}

with the actual work being done in the >operator of Ignorer<
Pred >.
More useful, probably, would be a manipulator which skips
using a regular expression.
Using a state-full predicate in the above would probably work
for that.
Not for the extended regular expressions of Boost. Those
require backtracking, and there's practically no way to
implement backtracking.

--
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

Feb 8 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ken Fettig | last post: by
reply views Thread by claire.bell1 | last post: by
9 posts views Thread by Dante | last post: by
43 posts views Thread by Steven T. Hatton | last post: by
15 posts views Thread by jaks.maths | last post: by
3 posts views Thread by news.microsoft.com | last post: by
1 post views Thread by sake | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.