Re: Is this code to find an int in a string okay ?
On Dec 6, 5:31 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "Diwa" <shettydiwa...@gmail.com> wrote in message
>
> news:85ec7280-977f-4651-a3f4-2199b4150d10@j44g2000hsj.googlegroups.com...
> > Hi Guys,
>
> > Is there any better way than below to find an int in a string (e.g.
> > "30" in "KFStat30A")
>
> > // --------------------------------------------------------------
> > #include <iostream>
> > #include <sstream>
> > #include <vector>
>
> > using namespace std;
>
> > int main()
> > {
> > vector<string> strs ;
> > strs.push_back("KFStat30A");
> > strs.push_back("KFStat2A");
> > strs.push_back("555555");
> > strs.push_back("KKKKKK");
> > strs.push_back("KKKK555");
>
> > std::string str;
>
> > for (int i=0; i<strs.size(); i++)
> > {
> > str = "";
>
> > int beg = strs[i].find_first_of("0123456789");
>
> > if (beg != string::npos)
> > {
> > int end = strs[i].find_first_not_of("0123456789", beg);
> > str.assign(strs[i], beg, end-beg);
> > }
>
> > cout << strs[i] << " " << str << "\n";
> > }
> > return 0;
> > }
>
> This code looks okay. Although I wonder what you would want with a string
> such as "KFStat30A02".
> - Show quoted text -
The strings come from a database. The "30" in that string represents
the 30 yr treasury bond. Only the first two strings (KFStat30A and
KFStat2A) were valid ones. I had put the other three strings just for
testing purpose.
|