Check if string contains another C

痴心易碎 提交于 2019-12-13 06:36:14

问题


Now i have this code, but it always set null

UNICODE_STRING str;
char *cmp = "Hello";

RtlInitUnicodeString (&str, L"Hello world!");

if( ( strstr((char * )str.Buffer, cmp) ) != NULL)
{
   // cmp founded in str.
}
else
{
   // cmp not founded in str. Always here, but why??
}

Can you explain me why strstr in my case always null?


回答1:


You're searching for a multi-byte string in a Unicode one. Use wcsstr:

wchar * cmp = L"Hello";
wcsstr(str.Buffer, cmp);

You were hiding this by casting to char *.


You should really ask another question for your second request, but you might write a function like this:

void make_string_lower(WCHAR * str)
{
  while(str[0] != '\0') {
    if(iswalpha(str[0] && !iswlower(str[0]))) {
      str[0] = towlower(str[0]);
    }

    str++;
  }
}

Alternatively use _wcslwr.



来源:https://stackoverflow.com/questions/9656245/check-if-string-contains-another-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!