Removing spaces?

I'm writing a program and part of it requires me to remove a bunch of spaces from the beginning of a string but still keep all the remaining characters.
Everything I've tried has given me completely empty strings as results.
Can anyone give me a code sample that will do this? Thanks.
You can use the find_first_not_of, and erase member functions to find the first character which isn't a space and remove everything before it

eg:
YourString.erase( 0, YourString.find_first_not_of(' ') ); );

http://www.cplusplus.com/reference/string/string/find_first_not_of.html
http://www.cplusplus.com/reference/string/string/erase.html



(Edit) I've noticed an error in the code I posted, the extra ');'
Last edited on
I can't really use that one because the strings will not always have spaces at the start of the string, and there could be spaces later in the string. I only need to remove spaces from strings that begin with a space and then only the leading spaces.
The above code still works by finding the first non-space. If there is no first non-space, then the string is either empty or all whitespace, in either case of which the resulting string will be empty (which is what you want).
What about a sample code that removes ALL whitespace in a input stream?

And is there a way to NOT remove whitespaces if the input is inside a certain string like '#' such as #hello world#
http://en.wikipedia.org/wiki/Whitespace_(computer_science)#Unicode

For the second point, that's already in the field of parsing.
Topic archived. No new replies allowed.