Home > Back-end >  How to search a substring of a LPWSTR?
How to search a substring of a LPWSTR?

Time:09-16

Is there any function that is used for searching a substring of LPWSTR?

LPWSTR a_string = _T("abcdef");
    
if (a_string .find(L"def") != std::string::npos)
{
    
}

CodePudding user response:

That initialization of a_string is invalid, LPWSTR can't be initialized pointing to a string literal, you need LPCWSTR.

This being a glorified pointer to wchar_t, or more accurately a macro that will end up expanding to wchar_t*, it does not have member methods, it is not a class like std::string. You will need to do it another way, like the one pointed out by Dialecticus, example:

LPCWSTR a_string = L"abcdef"; 
// or better yet
// auto a_string = L"abcdef"; 

if (wcsstr(a_string, L"def"))
{
    //...
}

You can avoid this altogether, if possible, by using std::wstring like pointed out by Panagiotis Kanavos in the comment bellow.

  • Related