名稱空間
變體
操作

wcsncmp

來自 cppreference.com
< c‎ | string‎ | wide
在標頭檔案 <wchar.h> 中定義
int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, size_t count );
(自 C95 起)

比較兩個空終止寬字串的最多 count 個寬字元。比較是按字典順序進行的。

結果的符號是所比較字串中第一對不同寬字元值的差的符號。

如果 lhsrhs 不是指向空終止字串的指標,則行為未定義。

目錄

[編輯] 引數

lhs, rhs - 指向要比較的以空字元結尾的寬字串的指標
count - 要比較的最大字元數

[編輯] 返回值

如果 lhs 在字典順序中出現在 rhs 之前,則返回負值。

如果 lhsrhs 比較相等,則返回零。

如果 lhs 在字典順序中出現在 rhs 之後,則返回正值。

[編輯] 注意

wcscollwcsxfrm 不同,此函式不區分割槽域設定。

[編輯] 示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
 
void demo(const wchar_t *lhs, const wchar_t *rhs, int sz)
{
    int rc = wcsncmp(lhs, rhs, sz);
    if(rc == 0)
        printf("First %d characters of [%ls] equal [%ls]\n", sz, lhs, rhs);
    else if(rc < 0)
        printf("First %d characters of [%ls] precede [%ls]\n", sz, lhs, rhs);
    else if(rc > 0)
        printf("First %d characters of [%ls] follow [%ls]\n", sz, lhs, rhs);
}
 
int main(void)
{
    const wchar_t *str1 = L"안녕하세요";
    const wchar_t *str2 = L"안녕히 가십시오";
 
    setlocale(LC_ALL, "en_US.utf8");
    demo(str1, str2, 5);
    demo(str2, str1, 8);
    demo(str1, str2, 2);
}

輸出

First 5 characters of [안녕하세요] precede [안녕히 가십시오]
First 8 characters of [안녕히 가십시오] follow [안녕하세요]
First 2 characters of [안녕하세요] equal [안녕히 가십시오]

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.4.4.3 wcsncmp 函式 (p: 434)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.4.4.3 wcsncmp 函式 (p: 380)

[編輯] 另請參閱

(C95)
比較兩個寬字串
(函式) [編輯]
比較兩個陣列中一定數量的寬字元
(函式) [編輯]
根據當前語言環境比較兩個寬字串
(函式) [編輯]
C++ 文件 中的 wcsncmp