共用方式為


strncmp、wcsncmp、_mbsncmp、_mbsncmp_l

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

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbsncmp_mbsncmp_l。如需詳細資訊,請參閱 /ZW 不支援 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 (定義在 <string.h> 和 <mbstring.h> 中)。

備註

strncmp 函式會執行 string1 和 string2 中最多第一個 count 字元的序數比較,並傳回指出子字串之間關聯性的值。 strncmp 是 _strnicmp 的區分大小寫版本。 wcsncmp 和 _mbsncmp 是 _wcsnicmp 和 _mbsnicmp 的區分大小寫版本。

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

此外,_mbsncmp 和 _mbsncmp_l 也會驗證參數。 若 string1 或 string2 為 null 指標,則會叫用無效參數處理常式,如參數驗證中所述。 如果允許繼續執行,則 _mbsncmp 和 _mbsncmp_l 會傳回 _NLSCMPERROR,且 errno 設為 EINVAL。 strncmp 和 wcsncmp 不會驗證其參數。 除此之外,這些函式的行為相同。

比較 _mbsncmp 和 _mbsncmp_l 的行為會受到地區設定的 LC_CTYPE 分類設定所影響。 這會控制對多位元組字元的開頭和結尾位元組的偵測。 如需詳細資訊,請參閱 setlocale。 _mbsncmp 函式會針對這個與地區設定相依的行為,使用目前的地區設定。 _mbsncmp_l 函式完全相同,不過它改用 locale 參數。 如需詳細資訊,請參閱地區設定。 如果地區設定是單一位元組的地區設定,則這些函式的行為等同於 strncmp。

一般文字常式對應

TCHAR.H 常式

未定義 _UNICODE 和 _MBCS

_MBCS 已定義

_UNICODE 已定義

_tcsnccmp

strncmp

_mbsncmp

wcsncmp

_tcsncmp

strncmp

_mbsnbcmp

wcsncmp

_tccmp

巨集或內嵌函式的對應

_mbsncmp

巨集或內嵌函式的對應

不適用

不適用

_mbsncmp_l

不適用

需求

常式

必要的標頭

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

.NET Framework 對等用法

System::String::Compare

請參閱

參考

字串操作 (CRT)

地區設定

多位元組字元序列的解譯

_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