high 0n C
high 0n C

Reputation: 57

How to grab values before space and after a space in c++

For a project im working i want to grab text before the space and after the space.

to find the space i use the isspace method, any ideas how to get this done

Upvotes: 2

Views: 244

Answers (1)

Nemanja Boric
Nemanja Boric

Reputation: 22157

Like 0x499602D2 noted in the commments, you can use std::stringstream to read from string just as you would do with any other stream (like std::cin):

#include <sstream>
#include <iostream>
#include <string>

int main()
{
    int a, b;
    std::stringstream ss(std::stringstream::in | std::stringstream::out);
    ss << "12 14";

    ss >> a;
    ss >> b;

    std::cout << a << std::endl;
    std::cout << b << std::endl;


    return 0;
}

Nice thing about this is that if you know how to work with std::cin or any other stream, you know how to work with stringstream, so you can adapt this example for you needs.

Upvotes: 3

Related Questions