3

I have a string like this:

aaa bbb

There is a space before the 2nd part of the string. My goal is to parse only the first part, so aaa. Everything after the space is out. How can I do this in C++?

4
  • 1
    possible duplicate of How do I tokenize a string in C++? Commented Nov 24, 2011 at 11:07
  • 1
    @KarlKnechtel: wrong dup / do not close He wants the first part, not all. General tokeniziation is an unnecessary performance waste here. Commented Nov 24, 2011 at 11:21
  • @phresnel I agree, but while this seems like exactly the sort of thing that's constantly asked, I couldn't find a better duplicate. Commented Nov 24, 2011 at 11:22
  • 1
    @KarlKnechtel: If you couldn't, then it isn't, imho. Commented Nov 24, 2011 at 11:23

4 Answers 4

9
std::string s = "aaa bbb";
std::string s_before_space = s.substr(0, s.find(' '));
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best solution. Smart, fast, clear and very useful. Thank you!
3
std::string s = "aaa bbb";

s = s.substr(0, s.find_first_of(' '));

Comments

3
 std::string s = "aaa bbb";
 std::istringstream ss(s);

 std::string token;
 if (ss>>token)   // or: while(ss>>token) for _all_ tokens
 { 
      std::cout << "first token only: " << token << std::endl;
 }

Alternatively, with a container and using <algorithm>

 std::string s = "aaa bbb";
 std::istringstream ss(s);

 std::vector<std::string> elements;
 std::copy(std::istream_iterator<std::string>(ss),
           std::istream_iterator<std::string>(),
           std::back_inserter(elements));

 // elements now contains the whitespace delimited tokens

Includes:

 #include <sstream>   // for ostringstream/istringstream/stringstream
 #include <algorithm> // for copy
 #include <iterator>  // for istream_iterator/back_inserter

5 Comments

But he only wants the first part, not all. General tokenization here = waste of performance.
I tried the second method of yours; the vector is empty after std::copy call.
@phresnel: then just make it if (ss>>token) { ... }? I'd prefer str.substr(0, str.find_first_of(" \t"))` though
@sehe: Like the first part better now :)
@jrok: oops, blatant typo - fixed now. Thanks for reporting!
-1

user following tokenizer, taken from some earlier post on this site.

void Tokenize(const std::string& str, std::vector<std::string>& tokens,const std::string& delimiters = " ") {
    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
    while (std::string::npos != pos || std::string::npos != lastPos){
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        lastPos = str.find_first_not_of(delimiters, pos);
        pos = str.find_first_of(delimiters, lastPos);
    }
}

1 Comment

But he only wants the first part, not all. General tokenization here = waste of performance. -1 because you just pasted without explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.