Share via


strncmp, wcsncmp, _mbsncmp, _mbsncmp_l

比較最多兩個字串的指定字元計數。

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbsncmp_mbsncmp_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

int strncmp(
   const char *string1,
   const char *string2,
   size_t count
);
int wcsncmp(
   const wchar_t *string1,
   const wchar_t *string2,
   size_t count
);
int _mbsncmp(
   const unsigned char *string1,
   const unsigned char *string2,
   size_t count
);
int _mbsncmp_l(
   const unsigned char *string1,
   const unsigned char *string2,
   size_t count,
   _locale_t locale
);int _mbsnbcmp(
   const unsigned char *string1,
   const unsigned char *string2,
   size_t count
);

參數

string1, string2
要比較的字串。

count
要比較的字元數。

locale
要使用的地區設定。

傳回值

傳回值,表示 string1 子字串和 string2 子字串的關聯,如下所示。

傳回值 描述
< 0 string1 子字串小於 string2 子字串
0 string1 子字串與 string2 子字串相同
> 0 string1 子字串大於 string2 子字串

在參數驗證錯誤上, _mbsncmp_mbsncmp_l 傳回 _NLSCMPERROR ,其定義于 和 <mbstring.h><string.h>

備註

strncmp 函式會執行 countstring1 中最多第一個 string2 字元的序數比較,並傳回指出子字串之間關聯性的值。 strncmp_strnicmp 的區分大小寫版本。 wcsncmp_mbsncmp_wcsnicmp_mbsnicmp 的區分大小寫版本。

wcsncmp_mbsncmp 分別是 strncmp 的寬字元版本和多位元組字元版本。 的引數 wcsncmp 是寬字元字串。 的引數 _mbsncmp 是多位元組字元字串。 _mbsncmp 會根據多位元組字碼頁,辨識多位元組字元序列,並在發生錯誤時傳回 _NLSCMPERROR

此外,_mbsncmp_mbsncmp_l 也會驗證參數。 如果 string1string2 為 Null 指標且 count 不等於 0,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,則 _mbsncmp_mbsncmp_l 會傳回 _NLSCMPERROR,且 errno 設為 EINVALstrncmpwcsncmp 不會驗證其參數。 除此之外,這些函式的行為相同。

比較 _mbsncmp_mbsncmp_l 的行為會受到地區設定的 LC_CTYPE 分類設定所影響。 這會控制對多位元組字元的開頭和結尾位元組的偵測。 如需詳細資訊,請參閱setlocale_mbsncmp 函式使用目前的地區設定進行這項與地區設定相關的行為。 _mbsncmp_l 函式相同,但會改用 locale 參數。 如需詳細資訊,請參閱 Locale。 如果地區設定是單一位元組的地區設定,則這些函式的行為等同於 strncmp

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcsnccmp strncmp _mbsncmp wcsncmp
_tcsncmp strncmp _mbsnbcmp wcsncmp
_tccmp 巨集或內嵌函式的對應 _mbsncmp 巨集或內嵌函式的對應

需求

常式 必要的標頭
strncmp <string.h>
wcsncmp <string.h><wchar.h>
_mbsncmp, _mbsncmp_l <mbstring.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_strncmp.c
#include <string.h>
#include <stdio.h>

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

int main( void )
{
   char tmp[20];
   int result;
   printf( "Compare strings:\n      %s\n      %s\n\n",
           string1, string2 );
   printf( "Function:   strncmp (first 10 characters only)\n" );
   result = strncmp( string1, string2 , 10 );
   if( result > 0 )
      strcpy_s( tmp, sizeof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, sizeof(tmp), "less than" );
   else
      strcpy_s( tmp, sizeof(tmp), "equal to" );
   printf( "Result:      String 1 is %s string 2\n\n", tmp );
   printf( "Function:   strnicmp _strnicmp (first 10 characters only)\n" );
   result = _strnicmp( string1, string2, 10 );
   if( result > 0 )
      strcpy_s( tmp, sizeof(tmp), "greater than" );
   else if( result < 0 )
      strcpy_s( tmp, sizeof(tmp), "less than" );
   else
      strcpy_s( tmp, sizeof(tmp), "equal to" );
   printf( "Result:      String 1 is %s string 2\n", tmp );
}
Compare strings:
      The quick brown dog jumps over the lazy fox
      The QUICK brown fox jumps over the lazy dog

Function:   strncmp (first 10 characters only)
Result:      String 1 is greater than string 2

Function:   strnicmp _strnicmp (first 10 characters only)
Result:      String 1 is equal to string 2

另請參閱

字串操作
地區設定
多位元組字元序列的解譯
_mbsnbcmp, _mbsnbcmp_l
_mbsnbicmp, _mbsnbicmp_l
strcmp, wcscmp, _mbscmp
strcoll 函數
_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