0

According to: C++ How to get substring after a character?

x.substr(x.find(":") + 1);

How can I do the same, but with whitespace ? because find() ignores spaces.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 3
    What have you tried to solve your homework? – Ulrich Eckhardt Feb 15 '18 at 20:37
  • 5
    @Vettel: "*`find()` ignores spaces*" - no, it doesn't. `x.substr(x.find(" ") + 1);` works just fine. Also `x.find(' ')` works, too – Remy Lebeau Feb 15 '18 at 20:37
  • 1
    The `find` member function does **not** ignore the whitespace. – Ron Feb 15 '18 at 20:38
  • 1
    @MayankJain: "*Stringstream let you specify delimiters*" - no, it doesn't. You might be thinking of `std::getline()` instead, which has an optional delimiter, and can use a stringstream as input. – Remy Lebeau Feb 15 '18 at 20:39
  • 1
    You should take a look [here](https://stackoverflow.com/questions/15475744/string-find-is-not-finding-spaces) – Eugene Sh. Feb 15 '18 at 20:40
  • 1
    @vettelsebastian why dont you just try it and see what happens? find(" ") – AndersK Feb 15 '18 at 20:41
  • @EugeneSh.: the problem with that question is that the user wasn't reading strings with spaces in them to begin with. That was a problem with using `cin >> ...` to read strings, not with using `find()` to find space characters. – Remy Lebeau Feb 15 '18 at 20:41
  • @RemyLebeau Yes. So I would imagine the OP has a similar problem. – Eugene Sh. Feb 15 '18 at 20:42

1 Answers1

4

If you want white space you need to use find_if() with an appropriate lambda that calls is::space()

auto pos = find_if(std::begin(x), std::end(x), [](unsigned char c){return std::isspace(c);});
pos++;
for(;pos!=x.end();pos++)
cout << *pos;
amar_1995
  • 575
  • 3
  • 14