How to check if a string contains a char?

Robert Lewis picture Robert Lewis · Apr 26, 2017 · Viewed 103.4k times · Source

I have a text file that I want to read. I want to know if one of the lines contains [ so I tried :

if(array[i] == "[")

But this isn't working.

How can I check if a string contains a certain character?

Answer

thibsc picture thibsc · Apr 26, 2017

Look at the documentation string::find

std::string s = "hell[o";
if (s.find('[') != std::string::npos)
    ; // found
else
    ; // not found