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 8 4601
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.
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
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;
}
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
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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken Fettig |
last post by:
I have a quick simple question. How do you convert an integer to a string in
Python?
Thanks
Ken Fettig
|
by: claire.bell1 |
last post by:
Hi,
I have an int and a char array in my program, the int is multiple digits.
How can I append the int to the char array/string?
This is not for use in cout or cin so the << and >> commands...
|
by: Dante |
last post by:
I'm converting a C# program to VB.net, but i'm having
problems converting a integer to a byte in the same way as
the c# program does.
//C# program
int i = 137694;
byte b = (byte) i;
//b...
|
by: Steven T. Hatton |
last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string
Is there no C library function that will take an int and convert it to its
ascii representation? The example Bjarne shows in his faq...
|
by: jaks.maths |
last post by:
How to convert negative integer to hexadecimal or octal number?
Ex: -568
What is the equivalent hexadecimal and octal number??
|
by: news.microsoft.com |
last post by:
Hello,
I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue.
Steping through both the C# and VB.NET apps i get identical results but for
one section.
Notes: bit_buffer = 360...
|
by: hapa |
last post by:
I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below
I have written this program to convert a integer to binary digit
problem one is that...
|
by: sake |
last post by:
This problem seems to have a fairly simple solution in C, and google seems to love to pull up solutions for C. However, C++ just seems have the exact opposite luck.
So pretty much, I want to...
|
by: tragic54 |
last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
|
by: geeta719 |
last post by:
void main()
{
char ch="";
char c;
int k=0,l,i;
printf("Enter a number:");
scanf("%d",&i);
while(i!=0) //reversing a numbr
{ k=k*10+i%10;
i=i/10;}
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |