Check if WCHAR contains a string

I have a WCHAR sDisplayName[1024]; variable WCHAR sDisplayName[1024];

How to check if sDisplayName contains the string "example"?

+4
source share
3 answers
 if(wcscmp(sDisplayName, L"example") == 0) ; //then it contains "example" else ; //it does not 

This does not apply if the line in sDisplayName starts with "example" or has an "example" in the middle. For these cases, you can use wcsncmp and wcsstr .

This check is also case sensitive.

Also, it will break if sDisplayName contains garbage - i. e. does not end with zero.

Use std :: wstring instead. This is a C ++ way.

EDIT: if you want to match the beginning of the line:

 if(wcsncmp(sDisplayName, L"Adobe", 5) == 0) //Stars with "Adobe" 

If you want to find the line in the middle

 if(wcsstr(sDisplayName, L"Adobe") != 0) //Contains"Adobe" 

Note that wcsstr returns a nonzero value if a string is found, unlike the others.

+12
source

You can use the wchar_t variants of the standard C functions (i.e. wcsstr ).

+1
source

wscstr will find your string anywhere in sDisplayName, wsccmp will see if sDisplayName is your string.

0
source

All Articles