How do you search a std::string for a substring in C++?

后端 未结 6 780
南方客
南方客 2020-12-10 16:34

I\'m trying to parse a simple string in C++. I know the string contains some text with a colon, followed immediately by a space, then a number. I\'d like to extract just t

6条回答
  •  抹茶落季
    2020-12-10 17:21

    I can't just tokenize on the space (using sstream and <<) because the text in front of the colon may or may not have spaces in it.

    Right, but you can use std::getline:

    string not_number;
    int number;
    if (not (getline(cin, not_number, ':') and cin >> number)) {
        cerr << "No number found." << endl;
    }
    

提交回复
热议问题