_stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l, _mbsicmp_l

执行不区分大小写的字符串比较。

重要

_mbsicmp_mbsicmp_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

int _stricmp(
   const char *string1,
   const char *string2
);
int _wcsicmp(
   const wchar_t *string1,
   const wchar_t *string2
);
int _mbsicmp(
   const unsigned char *string1,
   const unsigned char *string2
);
int _stricmp_l(
   const char *string1,
   const char *string2,
   _locale_t locale
);
int _wcsicmp_l(
   const wchar_t *string1,
   const wchar_t *string2,
   _locale_t locale
);
int _mbsicmp_l(
   const unsigned char *string1,
   const unsigned char *string2,
   _locale_t locale
);

参数

string1string2
要比较的 null 终止的字符串。

locale
要使用的区域设置。

返回值

返回值指示 string1string2 之间的关系,如下所示。

返回值 说明
< 0 string1 小于 string2
0 string1 等于 string2
> 0 string1 大于 string2

发生错误时,_mbsicmp 返回 _NLSCMPERROR,该返回值是在 <string.h><mbstring.h> 中定义的。

备注

_stricmp 函数在将 string1string2 的每个字符转换为小写之后对它们进行比较,并返回一个指示它们关系的值。 _stricmp_stricoll 的不同之处在于 _stricmp 比较仅受到会确定字符大小写的 LC_CTYPE 影响。 _stricoll 函数根据区域设置的 LC_CTYPELC_COLLATE 类别来比较字符串,其中该区域设置包括大小写和排序规则顺序。 有关 LC_COLLATE 类别的详细信息,请参阅 setlocale区域设置类别。 不带 _l 后缀的函数的版本会将当前区域设置用于区域设置相关行为。 带后缀的版本是相同的,只不过它们转而使用传入的区域设置。 如果未设置区域设置,则使用 C 区域设置。 有关详细信息,请参阅 Locale

注意

_stricmp 等效于 _strcmpi。 它们可以互换使用,但 _stricmp 是首选标准。

_strcmpi 函数等同于 _stricmp,且仅提供给向后兼容。

因为 _stricmp 进行小写比较,所以它可能导致意外行为。

为了说明何时通过 _stricmp 进行大小写转换会影响比较结果,假定你有两个字符串 JOHNSTONJOHN_HENRY。 字符串 JOHN_HENRY 被视为小于 JOHNSTON,因为“_”的 ASCII 值小于小写的 S。实际上,ASCII 值介于 91 和 96 之间的任何字符都将被视为小于任何字母。

如果使用 strcmp 函数而不是 _stricmpJOHN_HENRY 将大于 JOHNSTON

_wcsicmp_mbsicmp 分别是 _stricmp 的宽字符及多字节字符版本。 _wcsicmp 的自变量和返回值为宽字符字符串。 _mbsicmp 的自变量和返回值为多字节字符字符串。 _mbsicmp 根据当前的多字节代码页识别多字节字符序列,并在发生错误时返回 _NLSCMPERROR。 有关详细信息,请参阅代码页。 否则这三个函数否则具有相同行为。

_wcsicmpwcscmp 行为方式相同,只不过 wcscmp 不会在进行比较前将其参数转换为小写。 _mbsicmp_mbscmp 行为方式相同,只不过 _mbscmp 不会在进行比较前将其参数转换为小写。

需要为 _wcsicmp 调用 setlocale 才能使用拉丁语 1 字符。 默认情况下,C 区域设置生效,因此,例如,ä 不会比较等于 Ä。 在调用 setlocale 前,使用 C 区域设置以外的任何区域设置调用 _wcsicmp。 下面的示例演示了 _wcsicmp 如何受区域设置影响:

// crt_stricmp_locale.c
By default, this function's global state is scoped to the application. To change this behavior, see [Global state in the CRT](../global-state.md).

#include <string.h>
#include <stdio.h>
#include <locale.h>

int main() {
   setlocale(LC_ALL,"C");   // in effect by default
   printf("\n%d",_wcsicmp(L"ä", L"Ä"));   // compare fails
   setlocale(LC_ALL,"");
   printf("\n%d",_wcsicmp(L"ä", L"Ä"));   // compare succeeds
}

另一种方法是调用 _create_locale_wcreate_locale,并将返回的区域设置对象作为参数传递给 _wcsicmp_l

所有这些函数都验证其参数。 如果 string1string2 是空指针,则调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 _NLSCMPERROR 并将 errno 设置为 EINVAL

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsicmp _stricmp _mbsicmp _wcsicmp

要求

例程 必需的标头
_stricmp_stricmp_l <string.h>
_wcsicmp_wcsicmp_l <string.h><wchar.h>
_mbsicmp_mbsicmp_l <mbstring.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_stricmp.c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";

int main( void )
{
   char tmp[20];
   int result;

   // Case sensitive
   printf( "Compare strings:\n   %s\n   %s\n\n", string1, string2 );
   result = strcmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof(tmp), "less than" );
   else
      strcpy_s( tmp, _countof(tmp), "equal to" );
   printf( "   strcmp:   String 1 is %s string 2\n", tmp );

   // Case insensitive (could use equivalent _stricmp)
   result = _stricmp( string1, string2 );
   if( result > 0 )
      strcpy_s( tmp, _countof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, _countof(tmp), "less than" );
   else
      strcpy_s( tmp, _countof(tmp), "equal to" );
   printf( "   _stricmp:  String 1 is %s string 2\n", tmp );
}
Compare strings:
   The quick brown dog jumps over the lazy fox
   The QUICK brown dog jumps over the lazy fox

   strcmp:   String 1 is greater than string 2
   _stricmp:  String 1 is equal to string 2

另请参阅

字符串操作
memcmpwmemcmp
_memicmp_memicmp_l
strcmpwcscmp_mbscmp
strcoll 函数
strncmpwcsncmp_mbsncmp_mbsncmp_l
_strnicmp_wcsnicmp_mbsnicmp_strnicmp_l_wcsnicmp_l_mbsnicmp_l
strrchrwcsrchr_mbsrchr_mbsrchr_l
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l
strspnwcsspn_mbsspn_mbsspn_l