检查 WCHAR 是否包含字符串

check if WCHAR contains string

本文关键字:字符串 包含 是否 WCHAR 检查      更新时间:2023-10-16

我有变量WCHAR sDisplayName[1024];

如何检查sDisplayName是否包含字符串"示例"?

if(wcscmp(sDisplayName, L"example") == 0)
    ; //then it contains "example"
else
    ; //it does not

这不包括 sDisplayName 中的字符串以"example"开头或中间有"example"的情况。对于这些情况,您可以使用 wcsncmpwcsstr

此外,此检查区分大小写。

此外,如果sDisplayName包含垃圾,这将中断 - 即不以空结尾。

考虑改用 std::wstring。这是C++方式。

编辑:如果要匹配字符串的开头:

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

如果要在中间找到字符串

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

请注意,如果找到字符串,wcsstr 将返回非零值,这与其余字符串不同。

您可以使用标准 C 函数的wchar_t变体(即 wcsstr )。

wscstr 会在 sDisplayName 中的任何位置找到你的字符串,wsccmp 会查看 sDisplayName 是否正是你的字符串。