0

I'm a student so sorry if this is too basic of a question. I want to remove the whitespace at the start of a string, without removing the space between words.

stringstream parser(oneLine) ;
double amount ;
parser >> amount ;
string desc ;
getline(parser, desc) ;
cout << "Amount: $" << amount << " Desc: " << desc << endl ;

For example, I have this function I'm working on. It will read lines in from a text file, which are in the format of ex "10 Streaming Subscription."

The variables are currently: Amount = "10", desc =" Streaming Subscription" but I want desc to be "Streaming Subscription."

  • If the input is `10 ` followed by newline, do you want to read a blank desc, or keep reading the next line into desc? – M.M Oct 28 '20 at 20:33
  • @François Unhelpful knee-jerk dupe-closing, that; there is context here which makes that _not_ the best solution. – Asteroids With Wings Oct 28 '20 at 20:35
  • @AsteroidsWithWings See the second answer on the dupe, there is a breakdown of the solutions that directly addresses the OP's question. – cigien Oct 28 '20 at 20:37
  • @cigien No, none of those are appropriate in the OP's case. – Asteroids With Wings Oct 28 '20 at 20:37
  • @cigien Here we are reading from a stream so there are at least two options: (a) ignore whitespace on the stream and then getline the rest of the line; or (b) getline the rest of the line and then do an ltrim. The "duplicate" only applies to case (b) – M.M Oct 28 '20 at 20:39
  • Why is this [answer](https://stackoverflow.com/a/21815483/8372853) not appropriate? It shows how to remove leading whitespace. – cigien Oct 28 '20 at 20:39
  • @cigien You shouldn't remove anything; you should avoid having it in the first place. Read my answer, and think about the OP's problem as more than just the sequence of words in their title... – Asteroids With Wings Oct 28 '20 at 20:39
  • 1
    @AsteroidsWithWings Definitely not a knee-jerk. The duplicate is appropriate for the question. Though there may be better answers for the specific example cited the question, the title and body both agree that the main goal is to trim white space from strings which is distinct from skipping white space from a stream. In that optic the examples still hold in that they produce strings that need to be trimmed. If this is not the case OP can leave a comment or edit the question to make the alternative intention more explicit. – François Andrieux Oct 28 '20 at 20:43
  • @M.M I've seen worse... Hopefully they're more interested in the fact that they actually got a useful answer despite the efforts of some. – Asteroids With Wings Oct 28 '20 at 20:49

1 Answers1

0

You can do it with a search and an erase:

const std::size_t pos = desc.find_first_not_of(' ');
if (pos != std::string::npos)
   desc.erase(0, pos);

I wouldn't, though; erasing from the start of a string is not super-trivial, and you don't really need to do it.


I'd put parser.ignore(std::numeric_limits<std::streamsize>::max(), ' ') before your getline call instead, so the spaces never get read in the first place.

Note that if your input file is not formatted as you've described, this line will just keep looking for a space and chucking everything away until it finds one: if you think you may need to be more flexible, it's time to std::getline the whole line in the first place then parse it to an amount and a description after-the-fact.


Note that both solutions only consider space (U+0020) characters; you can expand it to care about e.g. tabspaces if you like. This is easy for the first approach, and slightly more complex for the second approach; in either case, I'll leave it as an exercise to the reader.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • The tab character is a whitespace character; your suggestion of `parser.ignore` only ignores the space character and not other whitespace (and it's not clear how it would be expanded to include all whitespace) – M.M Oct 28 '20 at 20:38
  • @M.M I can call U+0020 something else if that would be clearer, though the odds of the OP actually caring about tabs here are remote; I only included it for completion, and support for that can be left as an exercise to the reader. – Asteroids With Wings Oct 28 '20 at 20:39
  • 1
    Answers should answer the question, not leave it as an exercise for the reader... Also, the first part of your answer justifies the closing-as-duplicate – M.M Oct 28 '20 at 20:41
  • @M.M No part of the question talks about tabspaces. I'll happily expand my answer if it's edited to do so. Also, no it doesn't. Also, I'm not going to get into a Stack Overflow argument here over what is and isn't close-worthy, so if that's all you're here for, you might as well move on... have a good night! – Asteroids With Wings Oct 28 '20 at 20:41
  • 1
    A simple `parser >> std::ws` will consume leading whitespace. – Blastfurnace Oct 28 '20 at 20:50
  • @Blastfurnace Unfortunately, somebody closed the question, so we cannot deliver more answers with great suggestions like that. :( – Asteroids With Wings Oct 28 '20 at 20:51