You are here:

C++/extract characters after a space in a string

Advertisement


Question
How do I extract a word after a space in a string?

Answer
the simplest way is to use a stringstream. for example,

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

int main()
{
     std::string str = "    this is a string containing spaces" ;

     std::istringstream stream( str ) ;

     std::string word ;

     stream >> word ;
     std::cout << "the  first word is: " << word << '\n' ;

     stream >> word ;
     std::cout << "and the second word is: " << word << '\n' ;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.