How to check if a string contains a char?
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?
Solution 1:
Look at the documentation string::find
std::string s = "hell[o";
if (s.find('[') != std::string::npos)
; // found
else
; // not found
Solution 2:
Starting from C++23 you can use std::string::contains
#include <string>
const auto test = std::string("test");
if (test.contains('s'))
{
// found!
}