函式
<cwchar>

wcsncmp

int wcsncmp (const wchar_t* wcs1, const wchar_t* wcs2, size_t num);
比較兩個寬字串的字元
此函式比較C寬字串wcs1wcs2的前num個字元。

此函式從比較每個寬字串的第一個字元開始。如果它們相等,則繼續比較後續的字元對,直到字元不同、遇到終止的空寬字元,或者直到兩個字串中num個字元匹配為止,以先發生的為準。

這是strncmp<cstring>)的寬字元對應函式。

引數

wcs1
要比較的 C 寬字串。
wcs2
要比較的 C 寬字串。
num
要比較的最大字元數。
size_t 是一個無符號整數型別。

返回值

返回一個表示寬字串之間關係的整數值
零值表示兩個字串中已比較的字元構成了相同的字串。
大於零的值表示第一個不匹配的字元在wcs1中的值大於在wcs2中的值;小於零的值表示相反的情況。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* wcsncmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  wchar_t wcs[][5] = { L"R2D2" , L"C3PO" , L"R2A6" };
  int n;
  wprintf (L"Looking for R2 astromech droids...\n");
  for (n=0 ; n<3 ; n++)
    if (wcsncmp (wcs[n],L"R2xx",2) == 0)
    {
      wprintf (L"found %ls\n",wcs[n]);
    }
  return 0;
}

輸出

Looking for R2 astromech droids...
found R2D2
found R2A6


另見