John Doe John Doe - 17 days ago 5
C++ Question

Parse string containing numbers into integers

How can I parse numbers 4648, 4649, 4650 from this string into three int variables without using regex.h?

* SEARCH 4648 4649 4650
a3 OK SEARCH completed

Answer

Try using std::istringstream:

static const std:string    test_string = "* SEARCH 4648 4649 4650";
char asterisk;
std::string label;
unsigned int value1, value2, value3;
std::istringstream input(test_string);
input >> asterisk >> label >> value1 >> value2;

Edit 1:
For inputing more than one number:

input >> asterisk >> label;
std::vector<unsigned int> numbers;
unsigned int value;
while (input >> value)
{
  numbers.push_back(value);
}
Comments