See std::string::find_first_not_of .
To find the position (index) of the first non-spatial character:
str.find_first_not_of(' ');
To find the position (index) of the first non-empty character:
str.find_first_not_of(" \t\r\n");
It returns str.npos if str empty or consists entirely of spaces.
You can use find_first_not_of to trim leading spaces:
str.erase(0, str.find_first_not_of(" \t\r\n"));
If you donβt want to strictly indicate which characters are considered spaces (for example, use the locale ), you can still use isspace and find_if more or less the way sbi originally proposed, but take care of negating isspace , for example:
string::iterator it_first_nonspace = find_if(str.begin(), str.end(), not1(isspace)); // eg number of blank characters to skip size_t chars_to_skip = it_first_nonspace - str.begin(); // eg trim leading blanks str.erase(str.begin(), it_first_nonspace);
vladr
source share