Home > Back-end >  Weird wstring.find() behavior | VS C Latest
Weird wstring.find() behavior | VS C Latest

Time:05-25

  • I'm trying to filter out names from some wstrings, I need to filter out a specific name, but for some reason the .find() functions doesn't work:
std::wstring buf;

if (buf.find(L"Josh") != std::wstring::npos && !buf.find(L"NotJosh") != std::wstring::npos)
{
    // I'm trying to find a specific wstring that contains "Josh" but not "NotJosh" 
    // yet the .find() func returns positive even if it finds "NotJosh"
    // even tho it's conditions is set to (!)
}

/// This one doesn't work either:

if (!buf.find(L"NotJosh") != std::wstring::npos)
{
    if (buf.find(L"Josh") != std::wstring::npos)
    {

    }
}

CodePudding user response:

Issue solved by @Jesper:

This looks fishy: !buf.find(L"NotJosh") != std::wstring::npos Why not just: buf.find(L"NotJosh") == std::wstring::npos

And @NathanOliver:

! has higher precedence then != so !buf.find(L"NotJosh") != std::wstring::npos should be !(buf.find(L"NotJosh") != std::wstring::npos)

Thanks Everyone

  • Related