check if WCHAR contains string
Asked Answered
S

3

11

I have variable WCHAR sDisplayName[1024];

How can I check if sDisplayName contains the string "example"?

Shawnshawna answered 28/6, 2012 at 20:6 Comment(0)
P
18
if(wcscmp(sDisplayName, L"example") == 0)
    ; //then it contains "example"
else
    ; //it does not

This does not cover the case where the string in sDisplayName starts with "example" or has "example" in the middle. For those cases, you can use wcsncmp and wcsstr.

Also this check is case sensitive.

Also this will break if sDisplayName contains garbage - i. e. is not null terminated.

Consider using std::wstring instead. That's the C++ way.

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

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

If you want to find the string in the middle

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

Note that wcsstr returns nonzero if the string is found, unlike the rest.

Pillowcase answered 28/6, 2012 at 20:11 Comment(1)
this does not works if (wcscmp(sDisplayName, L"Adobe") == 0) when sDisplayName = 0x0045e084 L"Adobe AIR"Shawnshawna
R
1

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

Ruction answered 28/6, 2012 at 20:11 Comment(0)
F
0

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

Frambesia answered 28/6, 2012 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.