samedi 25 septembre 2021

Find exact word match in C++ string

I have the following strings:

std::string s1 = "IAmLookingForAwordU and I am the rest of the phrase";
std::string keyWord = "IAmLookingForAword";

I want to know if the keyWord as an exact match in s1

I used:

   if ( s1.find(keyWord) != std::string::npos )
    {
        std::cout << "Found " << keyWord << std::endl;
    }

but the find function catches the IAmLookingForAword in IAmLookingForAwordU and the if statement is set to true. However, I would like to only catch the exact match of the keyWork I am looking for.

Any way to do this with C++ strings?

Aucun commentaire:

Enregistrer un commentaire