【问题标题】:Check if string contains another C检查字符串是否包含另一个 C
【发布时间】:2012-03-11 15:27:51
【问题描述】:

现在我有了这段代码,但它总是设置为 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??
}

你能解释一下为什么我的 strstr 总是 null 吗?

【问题讨论】:

    标签: c string winapi unicode-string


    【解决方案1】:

    您正在搜索 Unicode 中的多字节字符串。使用wcsstr:

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

    您通过转换为 char * 来隐藏此信息。


    你真的应该为你的第二个请求问另一个问题,但你可能会写一个这样的函数:

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

    或者使用_wcslwr。

    【讨论】:

    • 我应该怎么做才能解决这个问题?
    • (offtop) 你能给我一个可以将 WСHAR 字符串替换为小写的函数吗?
    • @user1262425:添加了一些代码。没有测试,但希望能给你一些启发。
    • 此代码在技术上是不正确的,因为 UNICODE_STRING 不能保证为空终止。理想情况下,您需要将缓冲区转换回以空字符结尾的宽字符,然后进行比较。
    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2013-10-29
    相关资源
    最近更新 更多