Afficher un message
Vieux 06/12/2007, 15h51   #1
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is this code to find an int in a string okay ?

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

// --------------------------------------------------------------

Thanks
-- Diwa
  Réponse avec citation
 
Page generated in 0,06658 seconds with 9 queries