_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
);

パラメーター

string1, string2
Null で終わる比較対象の文字列。

locale
使用するロケール。

戻り値

戻り値は、次のように string1string2 の関係を示します。

戻り値 説明
< 0 string1string2 より小さい
0 string1string2 と同じ
> 0 string1string2 より大きい

エラーが発生した場合、_mbsicmp_NLSCMPERROR を返します。これは、<string.h> および <mbstring.h> で定義されています。

解説

この関数は_stricmp、各文字をstring1string2小文字に変換した後と比較し、その関係を示す値を返します。 _stricmp_stricoll の相違点は、_stricmp による比較では、どの文字が大文字または小文字であるかを決定する LC_CTYPE の影響のみを受けるという点です。 _stricoll 関数は、ロケールの LC_CTYPELC_COLLATE の両方のカテゴリに従って文字列を比較しますが、これには大文字と小文字、および照合順序の両方が含まれます。 カテゴリの詳細については、「ロケールカテゴリLC_COLLATE」を参照してくださいsetlocale _l サフィックスが付いていないこれらの関数のバージョンでは、ロケールに依存する動作に現在のロケールを使用します。 のサフィックスのある各バージョンは、代わりに渡されたロケールを使用することを除き、同じです。 ロケールが設定されていない場合は、C ロケールが使用されます。 詳細については、「 Locale」を参照してください。

Note

_stricmp_strcmpi と等価です。 この 2 つは区別なく使用できますが、推奨する標準は _stricmp です。

_strcmpi関数は _stricmpと同じですが、下位互換性を維持するために用意されています。

_stricmp は小文字の比較を実行するので、予期しない動作が発生する場合があります。

_stricmp による大文字と小文字の変換が比較の結果に与える影響について、JOHNSTONJOHN_HENRY という 2 つの文字列を使用して説明します。 "_" の ASCII 値は小文字の S よりも小さいので、JOHN_HENRYJOHNSTON よりも小さいとみなされます。実際、91 から 96 の ASCII 値を持つ文字はすべて、他の文字よりも小さいと判断されます。

strcmp 関数が _stricmp の代わりに使用される場合、JOHN_HENRYJOHNSTON より大きくなります。

_wcsicmp 関数と _mbsicmp 関数は、_stricmp 関数のワイド文字バージョンとマルチバイト文字バージョンです。 引数と戻り値 _wcsicmp はワイド文字列です。 引数と戻り値 _mbsicmp はマルチバイト文字列です。 _mbsicmp は現在のマルチバイト コード ページに基づいてマルチバイト文字のシーケンスを認識し、エラーが発生した場合は _NLSCMPERROR を返します 詳細については、「コード ページ」を参照してください。 それ以外では、これらの関数の動作は同じです。

_wcsicmpwcscmp 同じように動作しますが wcscmp 、引数を比較する前に引数を小文字に変換しない点が異なります。 _mbsicmp_mbscmp 同じように動作しますが _mbscmp 、引数を比較する前に引数を小文字に変換しない点が異なります。

ラテン 1 文字を使用するように呼び出すsetlocale_wcsicmp必要があります。 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 に渡す方法もあります。

これらのすべての関数では、パラメーターの検証が行われます。 null ポインターのstring2string1場合は、「パラメーターの検証」で説明されているように、無効なパラメーター ハンドラーが呼び出されます。 実行の継続が許可された場合、これらの関数は _NLSCMPERROR を返し、errnoEINVAL に設定します。

汎用テキスト ルーチンのマップ

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>

互換性の詳細については、「 Compatibility」を参照してください。

// 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