Share via


_stricmp, _wcsicmp, _mbsicmp, _stricmp_l, _wcsicmp_l, _mbsicmp_l

執行字串的不區分大小寫比較。

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbsicmp_mbsicmp_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 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
);

參數

string1, string2
以 Null 結束的待比較字串。

locale
要使用的地區設定。

傳回值

傳回值,表示 string1string2 的關聯,如下所示。

傳回值 描述
< 0 string1 小於 string2
0 string1 等於 string2
> 0 string1 大於 string2

在錯誤中, _mbsicmp_NLSCMPERROR 回 ,其定義于 和 <mbstring.h><string.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。事實上,任何在 91 到 96 之間具有 ASCII 值的字元都會被視為小於任何字母。

如果使用 函 strcmp 式而非 _stricmpJOHN_HENRY 將會大於 JOHNSTON

_wcsicmp_mbsicmp 分別是 _stricmp 的寬字元版本和多位元組字元版本。 的引數和傳回值 _wcsicmp 是寬字元字串。 的引數和傳回值 _mbsicmp 是多位元組字元字串。 _mbsicmp 根據目前的多位元組字碼頁來辨識多位元組字元序列,並在發生錯誤時傳回 _NLSCMPERROR。 如需詳細資訊,請參閱 字碼頁 。 除此之外,這三個函式的行為相同。

_wcsicmpwcscmp 的行為相同, wcscmp 不同之處在于,在比較引數之前,不會將其引數轉換成小寫。 _mbsicmp_mbscmp 的行為相同, _mbscmp 不同之處在于,在比較引數之前,不會將其引數轉換成小寫。

您必須呼叫 setlocale_wcsicmp 才能使用拉丁文 1 個字元。 C 地區設定預設會生效,因此,例如,ä 不會比較等於 Ä。 搭配 C 地區設定以外的任何地區設定呼叫 setlocale 會在 _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

這些函式全都會驗證它們的參數。 string1如果 或 string2 為 Null 指標,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 _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

另請參閱

字串操作
memcmp, wmemcmp
_memicmp, _memicmp_l
strcmp, wcscmp, _mbscmp
strcoll 函數
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l