Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the first character that is not whitespace in a std::string

Tags:

c++

string

stl

Lets say I have

std::wstring str(L"   abc");

The contents of the string could be arbitrary.

How can I find the first character that is not whitespace in that string, i.e. in this case the position of the 'a'?

like image 661
Christian Avatar asked Jan 10 '23 22:01

Christian


1 Answers

use [std::basic_string::find_first_not_of][1] function

std::wstring::size_type pos = str.find_first_not_of(' ');

pos is 3

Update: to find any other chars

const wstring delims(L" \t,.;");
std::wstring::size_type pos = str.find_first_not_of(delims);
like image 186
billz Avatar answered Jan 20 '23 04:01

billz