CLDY
CLDY

Reputation: 31

How do I extract an integer in a string?

I get a line like: "1001", Name

I want to know how to grab the number in between the quotes without atoi.

The problem asks to make the function just grab the integer that's between two quotes in a string, then grab the name and place it in a string, but I don't understand that part.

Upvotes: 2

Views: 61

Answers (3)

David C. Rankin
David C. Rankin

Reputation: 84521

You can also make use of the std::string functions find, find_first_not_of, and substr to parse the information.

You simply work your way down the original string finding the opening quote ", storing the index, then finding the closing quote, and its index, the integer string is the characters in between.

Next, you can use find_first_not_of locating the first character not a ", \t" (comma, space, tab), taking the name as the remainder of the original string.

#include <iostream>
#include <string>

int main (void) {

    std::string s = "\"1001\", Name", ssint, ssname;
    size_t begin, end;

    begin = s.find ("\"");                      /* locate opening quote */
    if (begin == std::string::npos) {           /* validate found */
        std::cerr << "error: '\"' not found.\n";
        return 1; 
    }
    end = s.find ("\"", begin + 1);             /* locate closing quote */
    if (end == std::string::npos) {             /* validate found */
        std::cerr << "error: closing '\"' not found.\n";
        return 1; 
    }
    ssint = s.substr (begin + 1, end - 1);      /* int is chars between */

    begin = s.find_first_not_of (", \t", end + 1);  /* find not , space tab */
    if (begin == std::string::npos) {           /* validate found */
        std::cerr << "error: no non-excluded characters found.\n";
        return 1; 
    }
    ssname = s.substr (begin);                  /* name is reamining chars */

    std::cout << "int : " << ssint << "\nname: " << ssname << '\n';
}

(note: always validate the results of find and find_first_not_of by ensuring the return was not std::string::npos)

Example Use/Output

$ ./bin/parse_str
int : 1001
name: Name

You can find details on all of the string library member functions at cppreference - std::basic_string Let me know if you have any questions.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595402

Have a look at std::istringstream, eg:

std::string s = "\"1001\", Name";
std::string name;
int num;

std::istringstream iss(s);
iss.ignore();
iss >> num;
iss.ignore();
iss.ignore();
std::getline(iss, name);

Or

std::string s = "\"1001\", Name";
std::string name;
int num;

std::istringstream iss(s);
iss.ignore(std::numeric_limits<std::streamsize>::max(), '"');
iss >> num;
iss.ignore(std::numeric_limits<std::streamsize>::max(), ',');
std::getline(iss, name);

Or

std::string s = "\"1001\", Name";
std::string name;
int num;

std::string::size_type start = s.find('"') + 1;
std::string::size_type end = s.find('"', start);
std::string snum = s.substr(start, end - start);

std::istringstream(snum) >> num;

start = s.find(',', end+1) + 1;
start = s.find_first_not_of(' ', start);
name = s.substr(start);

Upvotes: 0

Ali Tavakol
Ali Tavakol

Reputation: 439

Search using the regular expressions:

#include <regex>
#include <iostream>

int main()
{
    const std::string s = "\"1001\", John Martin";
    std::regex rgx("\"(\\d+)\", *([\\w ]+)"); // this will extract quoted numbers in any string
    std::smatch match;

    if (std::regex_search(s.begin(), s.end(), match, rgx))
        std::cout << "ID: " << match[1] << ", Name: " << match[2] << '\n';
}

Upvotes: 2

Related Questions