How to search a substring of a LPWSTR?

0

Issue

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)
{
    
}

Solution

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.

Answered By – anastaciu

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More